Trying To Access Variables From A Parent GameObject

Hello and good day!

Currently, I’m a bit stuck. I need a non-static instance of a script attached to a parent object that I can then access via another script in a child of that same parent.

Right now, this is what I have.

    public cubeAI Instance;

    void Awake ()
    {
        Instance = this;
    }

This is in the parent script.

    public cubeAI cubeInstance;

    void awake()
    {
        cubeInstance = gameObject.GetComponent<cubeAI>();
    }

This is in the child script.

And to say the least, it’s not quite working. I keep getting a null reference. Could anyone enlighten me on what I’m doing wrong? I can get this working by dragging and dropping in the parent into the instance in the inspector of the child during run-time, but this is obviously not a solution I’ll consider.
By the way, this parent and its children are being instantiated at run-time through some code, they’re not present when everything first loads up. I don’t know if that’s helpful at all, but I figured I would mention it regardless.

Thanks for your time, and have a great day!
-Hypocrita_20XX

GetComponent looks on the current gameobject… you want to look at the parent object

Hello LeftyRighty,

Thanks for your response!
Good news and bad news.
The good news is that your advice works perfectly!
The bad news is that it only works if it’s in the Update() function.
I’ll Investigate further, but I wanted to mention this just in case you or anyone else has any ideas as to what I need to correct.

Thanks again!
-Hypocrita_20XX

PS. Oh dear, major duh moment! I forgot to capitalize the “Awake” function…
Works perfectly now!