Why am I getting a NullReferenceException in this specific case? I'm lost :(

This is what I see when I start up the game: Imgur: The magic of the Internet The last error happens when I press “space”.

Ok, so I even put the “ResourcesScript” onto the “Cube” manually from the editor and it still can’t find it with “GetComponenet ();” :S The Actor class was initially used to create the component (and ITS reference to it exists and is not a null):

    void Awake () {
        resource = gameObject.AddComponent <ResourcesScript> ();
    }

This is the entire ResourcesScript:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ResourcesScript : MonoBehaviour {
    public GameObject attatchedGameObject;
    public Rigidbody attatchedRigidbody;

    public BehaviourStateResource behaviourResource;
    public BehaviourStateManager behaviourManager;

    public MovementStats moveStats;
    public MovementStatsManager moveManager;
    public State currentMovementState;
 
    public HealthStats healthStats;
    public HealthStats armorStats;
    public HealthStats shieldStats;
    //public HealthManager healthManager;

    void Start () {
        attatchedGameObject = gameObject;
        attatchedRigidbody = gameObject.GetComponent <Rigidbody> ();

        behaviourResource = new BehaviourStateResource ();
        behaviourManager = gameObject.AddComponent <BehaviourStateManager> ();

        moveStats = new MovementStats (new Stat (10.0f), new Stat (10.0f), 10.0f, new Stat (10.0f), new Stat (10.0f), 10.0f);
        moveManager = new MovementStatsManager (this);
        currentMovementState = gameObject.AddComponent <DefaultState> ();
     
        healthStats = new HealthStats (new Stat (10.0f), new Stat (10.0f), new Stat (10.0f));
        armorStats = new HealthStats (new Stat (10.0f), new Stat (10.0f), new Stat (10.0f));
        shieldStats = new HealthStats (new Stat (10.0f), new Stat (10.0f), new Stat (10.0f));
        //healthManager = gameObject.AddComponent <HealthManager> ();
    }
}

And finally, this is an example of a class that uses the GetComponent to get the object… but fails to find one:

public class BehaviourStateManager : MonoBehaviour {
    public ResourcesScript resource;

    void Start () {
        resource = gameObject.GetComponent <ResourcesScript> ();
        Utilities.Validate <ResourcesScript> (resource, this);
    }
}

I’ve been at this for a long time and I can’t seem to find the problem. I only got back into programming/scripting 2 weeks ago so the answer may be right under my nose :confused:

Please help,
Ryutsashi

from that screenshot, none of the errors appear to be about the resourcescript… can we see the code that is generating the errors rather than the class that might not exist?

Could you please show a snippet of the Actor and Jump State Class.

When I tried adding the ResourcesScript manually to the Cube gameObject through Unity editor, I commented out the “resource = gameObject.AddComponent ();” in the Actor class. Later I uncommented it and tried it with both adding the component through that script and manually adding it in the editor.

Debugging revealed that the “resource” variable in Actor class only stops being of value “null” AFTER the JumpState had run it’s entire Start() method, even though “resource = gameObject.AddComponent ();” in the Actor class gets run BEFORE JumpState’s Start() method. I have no idea why.

This is the ENTIRE actor class as it was in that screenshot:

using UnityEngine;
using System.Collections;

/*
* This is the base class for every actor in the game.
*
* Actors exhibit AI, have health, move, are interactable and use abilities.
* Of course, every actor is not the same (eg. a turret can rotate but can't move on its own and
* a ballistic bullet has no AI (though I'm still not sure if I want to make projectiles actors)).
*
*/

public class Actor : MonoBehaviour {
    public ResourcesScript resource;

    void Awake () {
        /*Debug.Log("'" + gameObject.name + "'" + " is my gameobject! <3");
        resource = gameObject.AddComponent <ResourcesScript> ();*/
    }

    void Start () {
        Utilities.Validate <ResourcesScript> (resource, this);
    }

    public virtual void Move () {

    }

    public virtual void Heal () {

    }

    public virtual void Damage () {

    }

    public virtual void InteractedWith () {

    }

    public virtual void UseAbility () {

    }
}

And this is the entire JumpState class as it was both then and now:

using UnityEngine;
using System.Collections;

public class JumpState : State {
    private ResourcesScript resource;

    void Start () {
        _ID = "JUMPING";
        info = "The actor is in free fall (maybe jumping??).";
        resource = gameObject.GetComponent <ResourcesScript> ();
        /*stateManager = resource.GetComponent <BehaviourStateManager>();
        if (stateManager == null) {
            Debug.Log (this.name + " didn't find an AIBehaviourStateManager in the parent GameObject. Fix it or this won't work bro. " + this.name + " will now destroy itself. *Poof!*");
            Destroy(this);
        }*/
        AddState();
    }
   
    protected override void AddState () {
        resource.behaviourResource.stateList.Add ( this );
        if (resource == null) { Debug.Log ("Test");}
    }

    public override void Command (string keyPressed) {
        switch (keyPressed) {
        default:
            break;
        }
    }
   
    public override void UpdateState () {
        if (!resource.behaviourResource.jumped && !resource.behaviourResource.grounded) {
            JumpAction jump = new JumpAction (resource, resource.attatchedGameObject.transform.up * resource.moveStats._acceleration.FinalStat * 5.0f); //needs to multiply up vector3 by resources.movestats.jumpForce
            resource.behaviourResource.jumped = true; //needs logic to set it false again later
            //destroy jump class
        }
    }
}

It’s probably down to script execution order
You need to make sure that scripts that AddComponents complete before other scripts try to GetComponent (If they are all using Start - you can use Awake to run before Start)

Thanks, yeah. I still don’t know why start in one component runs before the awake in another is finished but debugging shows that to be the case. It’s fixed now :slight_smile: