Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiated prefab loses script reference

Tags:

unity3d

My prefab will not retain the move joystick reference. If I add the reference back while running, the everything works, but the instantiated prefab will lost the reference once I delete it from the screen, or run the project.

I was sure to hit Apply on the prefab, but essentially, its acting as though I didn't.

the reference is added here...

enter image description here

but gone on the instantiated object...

enter image description here

Note that the ship object that I drag into the scene still has the reference, but the instantiated ship does not.

I've tried this with the joystick container as a prefab and not a prefab.

like image 279
tintyethan Avatar asked May 17 '18 21:05

tintyethan


1 Answers

This is expected behavior

Prefabs cannot maintain references to objects in the scene, as when they get instantiated, there's no guaranteed that that object still exists.

You will have to assign the reference to the script when you instantiate the prefab. You can do this by calling instance_obj.GetComponent<Move>().moveJoystick = ...

like image 131
Draco18s no longer trusts SE Avatar answered Sep 21 '22 14:09

Draco18s no longer trusts SE