- Home /
Any reason why script vanishes from prefab when instantiated?
I have a Player prefab. It has a few scripts on it - but one of them, when added to a prefab, is not there when the prefab instantiates. Only that script disappears, the rest are fine.
To work round, I call AddComponent() in OnStartServer, and all is then fine, but I'm just wondering what would cause a script to vanish from a prefab at instantiation time with no errors...
You probably Destroy it.
If you drag the prefab to scene, does it still exist there?
Yes it's there when I drag it over. But I'm not doing anything special with the components on that script after instantiation.
I've had another look, I can't see anything odd about it.
I put a debug statement into the Awake function for the vanishing script. Only time it gets called is when the manual AddComponent happens.
Answer by Ibzy · Jul 02, 2015 at 12:00 PM
Are you able to provide your Start() and Awake() functions of the offending script, and maybe a view of the inspector of the prefab?
With you using OnStartServer I imagine this is with UNET? Do you have the same prefab in the list of spawnable objects in your NetworkManager? I had a similar problem and found that it was set to a different prefab with a very similar name.
Ah ha. Thank you Ibzy for that.
I had a look at the Lobby$$anonymous$$anager. It had the "Game Player Prefab" set. However, the Spawn Info section didn't have anything set, and furthermore the Spawnable Prefabs didn't have the player set there.
I don't think they are actually needed, correct me if I'm wrong, but it seems like NetworkLobby$$anonymous$$anager for some reason uses its own m_GamePlayerPrefab field rather than using the Network$$anonymous$$anager.m_PlayerPrefab. But anyway, just to try, I added my Player prefab to both the Spawn Info section and the spawnable prefabs - and now - it worked - the script was there at instantiation.
However, then I took the prefabs out again (ie my Player Prefab was only set in "Game Player Prefab"), in order to narrow down exactly what fixed it. But it kept working... so it looks like something went weird with the Network$$anonymous$$anager prefab set up in the editor, and it corrected itself.
I think the Network$$anonymous$$anager stores a cache in the editor, and you will likely find the game crashes when you try to run it in standalone if you haven't set the spawnable prefab.
I spent about 4 days rolling back all sorts of changes to rectify the problem I had (the crashing) to eventually find this little dickens.
Glad it's sorted.
Answer by n1gth · Jul 02, 2015 at 08:40 PM
Looked like a issue with the editor, see my comment above. By tweaking the NetworkLobbyManager in the Inspector it resolved itself.
If I manage to reproduce I'll raise a bug report. It's now all working OK, even when I add extra scripts.