- Home /
Nested Prefabs at Runtime
I'm having an issue with nested prefabs currently.
This is the code causing the problem:
if(GUI.Button(new Rect(20,20,80,20), "Buy Burstfire Rifle")) {
GameObject burstfireRifle = (GameObject)Instantiate(burstfireRiflePrefab);
burstfireRifle.transform.parent = playerObject.transform;
}
It is tossing back the following error at runtime:
"Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption."
What it should be doing is taking a prefab for the burst fire rifle and making it a child of the player object (also a prefab, currently).
A few extra notes for reference:
The weapon prefabs are currently just an empty game object with a script attached that handles firing capabilities. I will probably need to make it both a script and a 3D model in the future, however, so I would think this needs to stay as a prefab rather than just adding a component script to the player prefab.
The player prefab is an empty game object containing all of the necessary scripts and physics objects and sounds with a single child containing the 3d Model (and animations). Doing this was required to fix animation errors.
I just need some what that I can ensure that whenever I move the player object, the weapon moves and rotates with it. If this can be done without parenting, that might work as well.
There will end up being hundreds or so equipment items in the end, so I would rather not place all of them on the prefab at start and enable them through the shop UI. There is also the possibility of multiple of the same item, meaning parenting at runtime is my current solution.
I've looked into the issue and it seems like nested prefabs is a wanted, but non-existant feature for Unity. I did see a few extensions that might solve the problem, but I would prefer not spending 50 or so bucks to find out there was another way to fix the issue.
I imagine having one instance of a prefab take on the orientation of another prefab is a common thing, so is there any way I can get around this limitation?
Willing to rework my idea, but I feel like there is something I must be overlooking here to make my current setup work.
Answer by DESTRUKTORR · Aug 17, 2012 at 02:48 AM
I believe what's happening here is that it's trying to edit the prefab, and not the instantiated object. Try using
GameObject burstRifPref = GameObject.Find("burstfireRiflePrefab(clone)");
or just finding the instantiated object in the hierarchy and manually setting it as a child of something else. I believe it will (temporarily) break the prefab, but at runtime... it really doesn't matter all that much, since unless the player will be creating/editing prefabs, they shouldn't need to be linked.
But seriously... keep the questions simpler, ok? Try to keep it to one problem at a time, and we don't need the life story of your entire project... just what you're directly trying to do right now, and what has and hasn't worked for you thus far, that directly pertains to what you're asking about. Not trying to be mean or anything, just... that was kinda a wall of text... I'm a little surprised I actually read it all...
Thanks, I'll give that a shot.
Also, I provided all information that might have been necessary. I felt it was simple enough to begin with :O If the solution wasn't easy, it might have been necessary to know what did or did not need to stay a prefab. Not sure what I could have removed without people potentially asking questions or giving unusable answers. Also, I'd rather someone give me extra information than post a "How I do this give me code plox" question.
Anyway, thank you for the reply. I was unaware my solution might have been trying to reference the prefab itself and not the created instance. This should work for me.
Got it. It was almost as you described, but ins$$anonymous$$d of finding the rifle clone, I needed to find the instance of the player unit and Unity was able to understand that the clone I created was already referencing an instance.
The only code that I had to add was a small amount in the Start() function:
void Start () { playerObject = GameObject.Find ("PlayerUnit"); if(playerObject == null) { Debug.Log ("Player is null"); } }
And it works perfectly now. I may move this code into Update() in case I ever destroy the player (probably not), but for now this will do.
Again, thanks for sending me in the right direction.
No problem :) and don't take my criticisms too seriously XD I was just sayin' it's usually better to give what you need to give, first (like code and a brief explanation as to what's going wrong) and then expand on that, as needed. Good luck in the future! :D