- Home /
Prefabs not remembering references scripts / gui elements
I'm in the process of creating a simple game as part of my college project. I've encountered an issue with prefabs and the fact that they don't seem to be remembering my references to scripts or gui elements (such as text). I have things such as pickups setup and test enemies that affect other scripts / gui elements when certain things happen such as an enemy being killed = the score increasing by 1. I'm not sure why this is happening as the objects I'm putting into the prefabs have the references / connections working fine.
How the script or GUI object is referenced in the code:
public ShootGun gunScript;
public Text aCounter;
Inspector view of the non-prefab object: http://i.imgur.com/qFxUC2H.png Inspector view of the prefab object: http://i.imgur.com/g6vsN3m.png
Any help would be greatly appreciated.
Prefabs remember references to objects that are part of the prefab; all other references will be lost.
Circumventing this is a broad topic. I call it "scene management" or "reference management".
The basic idea is, there's always a "most convenient" way to pass references around. Sometimes the script which spawns the instance can know and hand-off references to the new instance - this is usually the most straightforward case, but it isn't always convenient. Convenience is king.
$$anonymous$$ore complex scenes and games require more structured management. $$anonymous$$y current project has a nice tiered structure with an all-knowing singleton (GS$$anonymous$$ for game state manager) at the top who holds access to sub-tiers. I can drill down to whatever reference I want from anywhere in the project. e.g.
GS$$anonymous$$.Active$$anonymous$$ap.Entities.Agents.AnyRandomNearby( GS$$anonymous$$.Player.transform.position );
GS$$anonymous$$.Audio$$anonymous$$anager.PlayRandomFoleyOfType( Foley.Footstep );
GS$$anonymous$$.UI.Vendor.SellButton.GetChild(0).GetComponent<Text>().text = "whatever";
Again, this is a broad topic. Just remember, there's always a "most convenient" way to get the references you need.
Is this the most effective way to do it then?
Just to clarify, you have GS$$anonymous$$ as the overall parent of everything else in the scene, and within that you have other tiers for other elements (e.g. the player, the UI, etc) ? Little confused, sorry.
GS$$anonymous$$ isn't the parent of these objects, it's just a singleton which holds public references to them, which I expose as static properties. That's how I like to handle some aspects of reference management. This is something you'll want a bit later in your Unity career; don't sweat it until you need it, I guess.
Where it's convenient, I let the script responsible for spawning an object hand that new object the references it requires:
GameObject go = Instantiate(prefab) as GameObject;
go.GetComponent<$$anonymous$$yScript>().someVariable = someReferenceIHaveHere;
The "best" way to handle reference management is equal parts personal preference, convenience, and common sense.
Okay, I think I understand what you're saying now. If, for example, I instantiate a prefab of an enemy for my game and it should have a reference to GUI text on my interface that will update the score displayed by 1 each time an enemy is killed, how would I go about adding the reference in (doing the drag and drop into the inspector view) when I instantiate or create a new prefab for it?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers