Hello, I will start off by saying, I hate posting questions. I am 99% able to find the answer by either diligently searching google, or figuring it out. This time, I can’t find an answer, and I really don’t understand why it isn’t working. Here is my code, it’s all in the same script. OnCollisionEnter2D( collision : Collision){
if (collision.gameObject.tag == "PowerUp"){
var p : Transform;
p = collision.transform.GetComponent(PowerUpScript).powerUp; //Stores the power up
if (p != null){ //I hate this because of the error i'm getting
Debug.Log("Power up is " + p); //It returns the correct power up on pick up
ActivatePowerUp(p);
}
}
}
function ActivatePowerUp( pu : Transform ){
var powerUpScript : PowerUpScript = pu.GetComponent(PowerUpScript); //Saves the script on the powerup that defines what it does
Debug.Log("Pu = " + pu); //Once again returns the correct power up passed to it when it was called
if (powerUpScript.isWeapon){
ChangeWeapon(pu); //This script worked perfectly when i call it in start
}
if (powerUpScript.isShield){
//ActicateShield();
}
if (powerUpScript.isModifier){
//ActivateModifier();
}
}
The issue I’m getting is, when I collide with a power up i get this error in the console “NullReferenceException: Object reference not set to an instance of an object
PlayerControllerScript.ActivatePowerUp (UnityEngine.Transform pu) (at Assets/Scripts/PlayerControllerScript.js:127)
PlayerControllerScript.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Scripts/PlayerControllerScript.js:112)”
Which references this line if (powerUpScript.isWeapon){
. Now, I have even tried calling the script out of that variable like pu.GetComponent(PowerUpScript).isWeapon
but I still get the same error. Hope I was specific enough. Any help would be great, thank you!
EDIT: I just put made the ChangeWeapon(pu);
a comment (made it not call it) and it still returns the same error. So it’s definitely something up with accessing the pu value, even though it is able to use it a line before hand (with no problem and accurate).