Function variable loses value 2 lines into code *ANSWERED*

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).

Well it seems like. you need to assign the value powerUp on PowerUpScript

First you could check if the collision object has the script using

if(collision.transform.GetComponent(PowerUpScript)) // this means the object has the component powerupscript.

Then you could check the powerup value simply with

print(collision.transform.GetComponent(PowerUpScript).powerUp);

I fixed it! I was over complicating things. This is my final code that fixed it.

function OnCollisionEnter2D( collision : Collision2D){
	if (collision.gameObject.tag == "Ball"){
			Debug.Log("Level " + level);
		SceneManager.LoadScene(level); //If the player is hit by a ball, it restarts the level
	}
	if (collision.gameObject.tag == "PowerUp"){
		ActivatePowerUp ( collision.transform );
	}
}

function ActivatePowerUp( pu : Transform ){
	var powerUpScript : PowerUpScript = pu.GetComponent(PowerUpScript); //Saves the script on the powerup that defines what it does

	if (powerUpScript.isWeapon){
		ChangeWeapon(powerUpScript.powerUp);
		Debug.Log("Pu = " + pu);
	}
	//if (powerUpScript.isShield){
		//ActicateShield();
	//}
	//if (powerUpScript.isModifier){
		//ActivateModifier();
	//}
}

Basically what I did was when i called the ActivatePowerUp() I fed it the collisions objects transform directly. The problem was I was giving it a specific variable in the collisions script and then in the ActivatePowerUp script I was trying to get a boolean value of isWeapon within the powerUp variable which, like unity kept telling me, didn’t exist within that variable. I instead had to change the set up so powerUpScript equalled the collision objects actual PowerUpScript itself, and not a variable within it. From there I looked inside that script and asked if a specific variable was true. Then boom, it worked. Simple but complicated lol.

@grimmdeveloper thanks for the help man, you helped lead me in the right train of thought.