简体   繁体   English

Unity 实例化对象没有值

[英]Unity Instantiated Object doesnt have Values

I am trying to Instatiate Objects with the code attached but only the first Object has the Method awake called and doesent get any values out of orbitValues.我正在尝试使用附加的代码来实例化对象,但只有第一个对象具有唤醒方法,并且不会从轨道值中获取任何值。 The other Objects get instantiated but they also dont get any values.其他对象被实例化,但它们也没有得到任何值。 Can you help please?你能帮忙吗?

if (listChecked == true & objectsCreated == false)
        {
            objectsCreated = true;
            for (int i = 0; i < 5 + 1; i++)
            {
                Ellipse orbitValues = new Ellipse(float.Parse(orbitList[i].attributes.inc), float.Parse(orbitList[i].attributes.sma), float.Parse(orbitList[i].attributes.ecc), float.Parse(orbitList[i].attributes.aPer));
                Debug.Log("i: " + i + " aPer: " + orbitValues.aPer);
                var tempOrbiter = Instantiate(orbit, Vector3.zero, transform.rotation);
                tempOrbiter.GetComponent<OrbitMotion>().orbitPath = orbitValues;
                tempOrbiter.GetComponent<OrbitMotion>().id = i;
                tempOrbiter.transform.rotation = Quaternion.Euler(0, 0, orbitValues.inc);
                tempOrbiter.transform.parent = GameObject.FindGameObjectWithTag("OrbiterGroup").transform;
            }
        }

The class Ellipse:椭圆类:

public class Ellipse
{
    public float inc;
    public float sma;
    public float ecc;
    public float aPer;

    public float c;
    public float e;
    public float xAxis;
    public float zAxis;

    float x;
    float z;

    public Ellipse(float inc, float sma, float ecc, float aPer)
    {
        this.inc = inc;
        this.sma = sma;
        this.ecc = ecc;
        this.aPer = aPer;
    } ...

and the script that should be with every instantiated object:以及应该与每个实例化对象一起使用的脚本:

public class OrbitMotion : MonoBehaviour
{
    public Transform orbitingObject;
    public Ellipse orbitPath;
    public int id;

    [Range(0f,1f)]
    public float orbitProgress = 0f;
    public float orbitPeriod = 60f;
    public bool orbitActive = false;

    public float xOffset = 0;
    public float zOffset = 0;

    void Awake()
    {
        Debug.Log("id: " + id);
        Debug.Log(orbitPath.aPer);
        if (orbitingObject == null)
        {
            orbitActive = false;
            return;
        }

        orbitingObject = transform.GetChild(0).transform;
        transform.parent = GameObject.Find("OrbiterGroup").transform;

        transform.localRotation = Quaternion.Euler(0, 0, orbitPath.inc);
        transform.localRotation = Quaternion.Euler(0, orbitPath.aPer, 0);

        xOffset = (Mathf.Sin(orbitPath.aPer) * (orbitPath.sma - orbitPath.e));
        zOffset = (Mathf.Cos(orbitPath.aPer) * (orbitPath.sma - orbitPath.e));
        Debug.Log(orbitPath.e);
        Debug.Log(xOffset);
        
        transform.position = new Vector3(xOffset, 0, zOffset) / 1000;

        SetOrbitingObjectPosition();
        StartCoroutine(AnimateOrbit());

        gameObject.GetComponent<EllipseRenderer>().ellipse = orbitPath;
    }...

You're instantiating them, then Awake() gets called, then you update the script values then, before the next Update() call, Start() is called.您正在实例化它们,然后调用Awake() ,然后更新脚本值,然后在下一次Update()调用之前,调用Start()

Awake() is getting called on ALL of the instantiated objects, but it's getting called as soon as the prefab is instantiated, before you set any values. Awake()在所有实例化的对象上都被调用,但是一旦预制件被实例化,它就会在你设置任何值之前被调用。 This means that the id for all of them is the same, and it's defaulting to 0, so it probably just looks like the first one is getting called (especially if you're collapsing the notifications in the console window).这意味着它们所有的id都是相同的,并且默认为 0,因此它可能看起来就像第一个被调用(特别是如果您在控制台窗口中折叠通知)。

Moving the work to Start() gets the functionality you want because Start() gets called later, before the first Update() call.将工作移至Start()可以获得您想要的功能,因为Start()稍后会在第一次Update()调用之前被调用。 Awake() is invoked immediately on construction/instantiation.在构造/实例化时立即调用Awake() You don't have time to update anything between instantiation and Awake() getting called.您没有时间在实例化和Awake()被调用之间更新任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM