0

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;
    }...
3
  • Have you tried moving the code to Start instead? You could also have a function that accepts the orbit values as a param. Remove the constructor from OrbitMotion, it serves no purpose since MonoBehaviours are created using Instantiate and not new. Commented Jun 27, 2022 at 19:20
  • Thank you for the suggestions. Exchanging Awake with Start actually worked but I dont get why. Removed the Constructor befor but didnt update the script here sorry ;D Commented Jun 27, 2022 at 19:37
  • 1
    Awake happens when the component instance is being loaded. Think of awake as the faux constructor, only internal initialization should happen at this point in execution. Commented Jun 27, 2022 at 20:12

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.