- Home /
NRE when accessing another script's variable?
I'm not quite sure why this isn't working, if anybody can help me out I would be very grateful. This is the script I'm using:
using UnityEngine;
using System.Collections;
using AC;
public class ToggleCursorManager : MonoBehaviour {
void Start(){
PlayerInput playerInput = GameObject.FindGameObjectWithTag("PersistentEngine").GetComponent <PlayerInput>();
playerInput.cursorIsLocked = true;
}
}
Not to complicated, right? The error is on line 9:
NullReferenceException: Object reference not set to an instance of an object ToggleCursorManager.Start () (at Assets/Scripts/ToggleCursorManager.cs:9)
And just for good measure, here is the line defining the variable I'm trying to call (in another script):
[HideInInspector] public bool cursorIsLocked = false;
I'm completely confused as to what's going on with this. How do I fix this NRE? Thank you in advance for any help!
It looks like it is not finding the 'PlayerInput' component on the game object with the 'PersistentEngine' tag. Any chance that you (unintentionally) have that tag on more than one game object? Are you sure the 'PlayerInput' component is on that specific game object (not a child or parent for example)?
Hehe oops! Wrong gameobject I was referencing! (Copied this from one of my other scripts). Thanks!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can't load Mouse Cursor texture 1 Answer
RaycastHit2D not interacting with tags 1 Answer
NullReferenceException and m_InstanceID == 0 on LoadLevel (C#) 1 Answer