Home > OS >  Unity Instantiated Object doesnt have Values
Unity Instantiated Object doesnt have Values

Time:06-28

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;
    }...

CodePudding user response:

You're instantiating them, then Awake() gets called, then you update the script values then, before the next Update() call, Start() is called.

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. 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).

Moving the work to Start() gets the functionality you want because Start() gets called later, before the first Update() call. Awake() is invoked immediately on construction/instantiation. You don't have time to update anything between instantiation and Awake() getting called.

  • Related