Null Reference Exception on C# script
Im trying to get this pickup script working and I keep getting the Null Reference Exception Error for line 28
"NewWeapon.transform.SetParent(player.transform);"
As you can see New Weapon and player are both found in the script. New weapon is created and player is the object the script is attached to. So I cannot for the life of me figure whats coming out null.
If somebody could help me solve this it would be greatly appreciated.
using UnityEngine; using System.Collections; public class SwitchWithPickup : MonoBehaviour { %|-762413517_1|% %|-1848002003_2|% %|1814450058_2|% %|-905520308_3|% %|-805487823_5|% %|1668055449_6|% %|-2081906248_4|% %|-912623362_5|% %|-1988582474_6|% %|-523839565_7|% %|-1510337148_8|% %|-1690468637_9|% %|-1926582400_13|% %|882512063_1|% %|-1473455472_11|% %|-1003367730_16|% %|-1860349403_17|% %|-237406474_18|% %|308905561_19|% }
using UnityEngine;
using System.Collections;
public class SwitchWithPickup : $$anonymous$$onoBehaviour {
public string childThatWillBeReplaced;
GameObject weapon;
GameObject pickupTrigger;
GameObject player;
void Start () {
weapon = GameObject.Find (childThatWillBeReplaced);
player = this.gameObject;
}
void OnTriggerEnter(Collider other) {
pickupTrigger = other.gameObject;
if (other.CompareTag("pickup"))
{
other.tag="Weapon";
Destroy(weapon);
GameObject NewWeapon = Instantiate(other, transform.position, Quaternion.identity) as GameObject;
NewWeapon.transform.SetParent(player.transform);
Destroy(other.transform.gameObject);
}
}
}
Follow this Question
Related Questions
NullReferenceException when using gameObject.SetActive(false); 0 Answers
Help needed finding NullReferenceException 1 Answer
null reference exception 0 Answers
Random Chance NullReferenceException: Object reference not set to an instance of an object 2 Answers
NullReference.Exveption: Object reference not set to an instance of an object 1 Answer