I'm working on a simple "space shooter" game. I have a player prefab a player script attached. The following code takes place within my update.
`//Activate Shield
if (Input.GetKeyDown(shieldKeyCode)){
if (shieldOn == true)
{
}
else
{
shieldOn = true;
GameObject clone = Instantiate(shieldMesh, transform.position, transform.rotation) as GameObject;
clone.transform.parent = gameObject.transform;
}
}`
I had hoped that this would make the transform of my player prefab the parent of the shield mesh game object clone. However, when I run my game I get a "Null Reference Exception" error pointing to the final line of code.
I have tried using SetParent() to no avail as well.
End goal, I want the shield to instantiate upon key press, gaining a parent of the player's transform keeping the shield wherever the player is. As of right now it just instantiates a shield and leaves in where it was (while giving my NRE error).
↧