This question was
closed Apr 17, 2017 at 10:25 AM by
hexagonius for the following reason:
NullReferenceException: Object reference not set to an instance of an object
Why am i getting an "Object reference not set to an instance of an object" error ?
ScriptA
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class touchManager : MonoBehaviour {
public GameObject targetMarker;
void Start () {
}
}
And scriptB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class tileManager : MonoBehaviour {
touchManager tManager;
GameObject tMarker;
void Start() {
tMarker = tManager.targetMarker;
}
}
Comment
$$anonymous$$ake sure you assign the references in the hierarchy and check for duplicate script in the scene.
I have assigned the reference in the inspector and this are the only code and scripts.
did you make sure there is no duplicate script in the scene? If you assigned references then editor never throw exceptions.
Answer by UnityCoach · Apr 17, 2017 at 10:10 AM
You obviously didn't assign the tManager reference, as it is private and not marked as Serialised. You can either mark it as public :
public touchManager tManager;
or serialised
[SerializeField] touchManager tManager;
Then, assign it the touchManager you have assigned a gameObject to.