- Home /
Ammo Script gives "object reference not set to an instance of an object"
Hi, im working on a script that gives a gun += 20 ammo when picked up.
var spawnTime: float = 0;
var isCollider: boolean = true;
var ObjectToDeactivate : GameObject;
function OnTriggerEnter(Trigger : Collider){
var pickupScript: zlimeZooka = Trigger.GetComponent(zlimeZooka);
if(isCollider == true){
if(Trigger.tag == 'Player'){
pickupScript.zlimeAmmo += 20;
ObjectToDeactivate.SetActive(false);
spawnTime += 15;
isCollider = false;
}
}
}
function Update(){
if(spawnTime >= 0){
spawnTime -= Time.deltaTime;
}
if(spawnTime <= 1){
ObjectToDeactivate.SetActive(true);
isCollider = true;
}
}
What am i doing wrong?
Comment
Best Answer
Answer by Fattie · Dec 30, 2012 at 06:20 PM
Line 9 : pickupScript.zlimeAmmo += 20;
the answer is simple: "pickupScript" is not being set.
(A) Trigger : Collider
the variable name should be lowercase, "trigger".
(B) It is possible that the collider passed in does NOT have a zlimeZooka.js sript attached. Triple-check.
(C) Add this:
Debug.Log( "the name is " + trigger.gameObject.name );
to clarify what is happening
(D) Add this line of code (you need this always)
if ( trigger.GetComponent(zlimeZooka) == null )
Debug.Log("BIG PROBLEM .. no script zlimeZooka.js found:);
Hope it helps!
Your answer
Follow this Question
Related Questions
Script stops working when switching to Android 1 Answer
trouble adding script to clone object 1 Answer
getting the object that the running script is attached to 1 Answer
How do I add ammo and reloads? 1 Answer
reseting rotation of objects? 0 Answers