- Home /
Activate object with key
Hey guys, can anyone help me figure out what is happening here? I keep getting this error on the script. Driving me bananas. I apologize for the structure of the code, I'm trying to use notepad because mono will not open for some reason. Notepad is giving me a hard time, please help me figure out whats wrong. :)
devilFood.Awake () (at Assets/_myscripts/myScript.cs:29) public GameObject laser;NullReferenceException: Object reference not set to an instance of an object
public bool requireKey;
public AudioClip doorSwishClip;
public AudioClip accessDeniedClip;
private DoneHashIDs hash;
private GameObject player; private DonePlayerInventory playerInventory;
private int count;
void Awake () {
hash = GameObject.FindGameObjectWithTag(DoneTags.gameController).GetComponent(); player = GameObject.FindGameObjectWithTag(DoneTags.player); playerInventory = player.GetComponent(); } void OnTriggerStay (Collider other) { if(other.gameObject == player) { if(requireKey) { if(Input.GetButtonDown("Interact")) if(playerInventory.hasKey) LaserDeactivation(); count++; else { audio.clip = accessDeniedClip; audio.Play(); } } else count++; } else if(other.gameObject.tag == DoneTags.enemy) { if(other is CapsuleCollider) count++; } } void OnTriggerExit (Collider other) { if(other.gameObject == player || (other.gameObject.tag == DoneTags.enemy && other is CapsuleCollider)) count = Mathf.Max(0, count-1); } void LaserDeactivation () { laser.SetActive(true); audio.Play(); } }
If your going to omit brackets when using single line after an IF statement, can you please properly format your code.
You are missing a bracket.
Answer by joseques · Feb 23, 2014 at 07:12 AM
Check your brackets. Your missing open and closed brackets on line 28 and 35
Also I give you a link to format your C# code for better readability. http://www.manoli.net/csharpformat/
Here's your fixed and formated code
public GameObject laser;
public bool requireKey;
public AudioClip doorSwishClip;
public AudioClip accessDeniedClip;
private DoneHashIDs hash;
private GameObject player;
private DonePlayerInventory playerInventory;
private int count;
void Awake ()
{
hash = GameObject.FindGameObjectWithTag(DoneTags.gameController).GetComponent<DoneHashIDs>();
player = GameObject.FindGameObjectWithTag(DoneTags.player);
playerInventory = player.GetComponent<DonePlayerInventory>();
}
void OnTriggerStay (Collider other)
{
if(other.gameObject == player)
{
if(requireKey)
{
if(Input.GetButtonDown("Interact"))
{
if(playerInventory.hasKey)
LaserDeactivation();
count++;
}
else
{
audio.clip = accessDeniedClip;
audio.Play();
}
}
else
count++;
}
else if(other.gameObject.tag == DoneTags.enemy)
{
if(other is CapsuleCollider)
count++;
}
}
void OnTriggerExit (Collider other)
{
if(other.gameObject == player || (other.gameObject.tag == DoneTags.enemy && other is CapsuleCollider))
count = Mathf.Max(0, count-1);
}
void LaserDeactivation ()
{
laser.SetActive(true);
audio.Play();
}
}
Your answer
Follow this Question
Related Questions
Opening Door with Key 1 Answer
Door opening with a key, my scripts and problem 1 Answer
key to open gate 1 Answer
Press an in-game switch to open a door - help? 2 Answers
GUI text, door and object collider 2 Answers