0

This is what I see when I start up the game: https://i.stack.imgur.com/IpXkx.png 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.

d4Rk
  • 6,622
  • 5
  • 46
  • 60
  • 1
    IF you debug where does the exception get thrown? Can you also show the `JumpState.cs` – Jamie Rees Jul 22 '15 at 08:41
  • This is the JumpState class (there's more but it's too big to fit in here and none of it has anything to do with ResourcesScript or any of its methods or variables): void Start () { resource = gameObject.GetComponent (); AddState(); } protected override void AddState () { resource.behaviourResource.stateList.Add ( this ); if (resource == null) { Debug.Log ("Test");} } The exception is thrown exactly where you'd expect: in the "resource = gameObject.GetComponent ();" and for some reason resource is STILL null at that point. – Ryutsashi Jul 22 '15 at 09:16
  • "resource = gameObject.AddComponent ();" in the Actor class get's called in Actor's Awake() but only becomes something that isn't null after the JumpState has run through its Start() method already :S And I do know what a nullReffExcept is but the problem is that I can't see why it's being thrown here. I can't find an answer in "What is a NullReferenceException and how do I fix it?" thread linked above :( – Ryutsashi Jul 22 '15 at 09:20
  • when you do resource = gameObject.AddComponent (); you already take your ResourceScript instance on resource object, you don't need to do resource = gameObject.GetComponent (); again. BTW without see your trace is difficult to help, could you edit your question or make another with your console trace? I can't see images attached so if your linked image is about your console out forgive me... – Frohlich Jul 22 '15 at 13:02
  • I had an accident in the meantime and lost the project unfortunately ( :( *sigh*...). It doesn't matter anymore because, I'm trying a different approach this time anyway. By the way, the AddComponent(); and GetComponent(); we're in different classes that didn't have any connection between them so one class would add it and a new instance of another class would then search for the component – Ryutsashi Aug 05 '15 at 19:44

0 Answers0