- Home /
NullReferenceException
Hi I'm making a 2D platformer with guns.
I'm having trouble with my weapon pickup scripts. I want to 1. Give player a new weapon (gameObject). 2. Destroy the old weapon. 3. make references for the old weapon point to the instance of the new weapon.
I have a "WeaponPickup" class that hold a reference to the prefab. I want to pass this to a function on my "PlayerActions" -script.
It looks like this:
public void GiveWeapon(GameObject newWeapon)
{
Debug.Log(newWeapon);
GameObject gunClone;
gunClone = Instantiate(newWeapon, gunAP.position, Quaternion.identity) as GameObject;
if (!facingRight)
newWeapon.transform.localScale = new Vector3(-newWeapon.transform.localScale.x, newWeapon.transform.localScale.y, newWeapon.transform.localScale.z);
gun = newWeapon;
gun.transform.parent = gunAP;
gunProperties = gun.GetComponent<GunProperties>();
gP1 = gun.transform.Find("GP1");
}
I get this error: NullReferenceException: Object reference not set to an instance of an object NewPlayerController.GiveWeapon (UnityEngine.GameObject newWeapon) (at Assets/Scripts/Player/NewPlayerController.cs:389) WeaponPickup.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/WeaponPickup.cs:35)
Essentially I want to replace a child of my PlayerCharacter with a Prefab and have the new prefab "stick to" (become child of) my Player's Arm-GameObject.
PS the Debug.Log above correctly posts the name of the new Weapon GameObject passed from my "WeaponPickup" class.
Is gunAP assigned? Oh actually... I just saw, you instantiate GameObject gunClone as a GameObject, and a few lines down you're doing a GetComponent, it would seem to me, that at this point the gun doesn't actually have a GunProperties script on it, which is a probable cause for the null reference.
The prefab has the GunProperties script attached to it. The instanced clone should also have it no?
gunAP was not assigned properly, this caused the NullReference. Thanks for helping!
Answer by Kiwasi · May 12, 2014 at 07:25 PM
If line 4 returns a gun then your problem is not newWeapon.
Your problem is probably gunAP.position. Throw that into a debug statement and see what happens.
Things to check
Does gunAP exist?
Is gunAP a Transform?
Has gunAP been assigned a value?
Is gunAP declared in this script?
Is gunAP out of scope? Out of scope typically happens when it is declared inside another method. In general variables only exist until the close of the curly brackets they were declared in.
Has gunAP been destroyed by some other script?
Thanks, this was indeed the problem. gunAP was null, I had set it earlier using transform.find which returned null, since it was a child of a child of a child. I've set it ins$$anonymous$$d using a tag and that solved everything. Should've Debug.log'ed it but I was staring myself blind at the 'newWeapon' to be the culprit. $$anonymous$$uch appreciated!
Answer by Warenth · May 12, 2014 at 02:45 AM
From what I can see, it appears you are referencing newWeapon when you should be referencing gunClone. On this line:
gun.transform.parent = gunAP;
If gun is newWeapon, since you set it as so:
gun = newWeapon;
Then gun.transform might be null if newWeapon is a prefab that is not actually in your scene.
My guess is this script should be referencing gunClone instead of newWeapon after new weapon has been cloned.
Thanks for your answer, this is true, but still does not help. The error occurs before at:
gunClone = Instantiate(newWeapon, gunAP.position, Quaternion.identity) as GameObject;
my code is now:
public void GiveWeapon(GameObject newWeapon)
{
Debug.Log(newWeapon);
GameObject gunClone;
gunClone = Instantiate(newWeapon, gunAP.position, Quaternion.identity) as GameObject;
if (!facingRight)
gunClone.transform.localScale = new Vector3(-gunClone.transform.localScale.x, gunClone.transform.localScale.y, gunClone.transform.localScale.z);
gun = gunClone;
gun.transform.parent = gunAP;
gunProperties = gun.GetComponent<GunProperties>();
gP1 = gun.transform.Find("GP1");
}
Still same NullReferenceException. Is it somehow impossible to Instatiate a prefab that is passed from another class through a public function?