- Home /
How to assign a gameObject to prefab (in-game via code)?
Hi , I wondered if anyone knew how to assign a gameObject to a prefab via code in-game.
What I'm doing at the moment is letting the user make a car in one scene(garage) and I want to transfer the car to another scene(track) to instantiate it for multiplayer.
I have tried using- EditorUtility.CreateEmptyPrefab() -but this only works in the editor , I also read in order to do this you need to add this to Assets/Editor folder , this stopped the build error but didn't run the script.
Thanks in advanced for any help :)
Just don't destroy the object when you load the new scene. You can then either use the object directly or clone the object using Instantiate().
http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html
Would that work? , because I want to Network.Instantiate it so others can see it too.
I missed the 'multi-player' in your question. I've never written a network app, but I doubt this solution would work. Your prefab solution will not work either (partly because you cannot build prefabs at runtime and partly because it suffers from the same communication issue). $$anonymous$$y guess is that you will have to figure out how to communicate the assembly of the car to the other clients, and have their car assemble the game object...a bit of X$$anonymous$$L perhaps. But this is only a guess. I converted my answer into a comment so that someone who does network games can answer.
I agree with Rob's last comment, I would suspect that that you will want to SAVE the car to permanent storage (a file), so it can be used next time the player loads the game. If you this, you will also be able to send that save file over the network.
Answer by jimmycrazyskills · Jan 06, 2014 at 08:30 PM
Thanks I'll try this , although if you have any other ways of doing it please say :) thanks again