- Home /
Make similar objects the same prefab?
So i'm making a survival game and I randomly generated trees using Random.Range and Instantiate(), and it was done during playmode. So my question is, is there anyway to make the 1500 trees in my game all the same tree prefab, so if i change something on the prefab it will change it for all the trees?
How are you creating trees now? That's pretty vital information.
Instantiating from a prefab does exactly what you describe (if i understood correctly)
pragma strict
var tree : GameObject; var AmountTrees : int;
function Start() { InvokeRepeating("GenerateTrees", 0, 0.0001); }
function Update () { if (AmountTrees <= 0) { CancelInvoke("GenerateTrees"); } }
function GenerateTrees() { AmountTrees--; Instantiate(tree, Vector3(Random.Range(54.61, 419.4), 100, Random.Range(57.24, 437.3)), Quaternion.Euler(0,0,0)); }
The names appear white, so is there anyway to make them blue? And they're all attached to the same prefab
Answer by Bunny83 · Feb 21, 2017 at 06:35 PM
Don't use "Instantiate". Instantiate will just create a copy / clone of the source object. The "prefab link" is a pure editor feature. To preserve that link you have to use the editor method PrefabUtility.InstantiatePrefab inside an editor script. Again the concept of a prefab connection doesn't exist at runtime. Prefabs at runtime are just off-scene objects that can be cloned into the scene.
For this it might be a good idea to create a ScriptableWizard where you can select your prefab and set all the parameters (number of trees) and then generate them inside OnWizardCreate.
Thanks, you rock. In case anyone wants this :d. Delete the old ones on your own rather. using UnityEngine; using UnityEditor;
[ExecuteInEditMode]
public class ObejctSwapper : MonoBehaviour {
public GameObject Prefab;
public GameObject[] ObjectsToExchange;
public bool SwapNow;
void Update () {
if (SwapNow) {
for (int i = 0; i < ObjectsToExchange.Length; i++) {
GameObject newObject = (GameObject) PrefabUtility.InstantiatePrefab (Prefab);
//newObject.transform.parent = ObjectsToExchange [i].transform.parent;
newObject.transform.position = ObjectsToExchange [i].transform.position;
newObject.transform.rotation = ObjectsToExchange [i].transform.rotation;
newObject.transform.localScale = ObjectsToExchange [i].transform.localScale;
}
SwapNow = false;
}
}
}
Your answer

Follow this Question
Related Questions
how do i stop instantiate from changing a prefab 0 Answers
Dynamically Loading audio into a prefab and Instantiate in scene. 0 Answers
Unity 5 : Best way to initialize a UI panel coming from a prefab with many text fields ? 0 Answers
Prefabs won't change? 1 Answer
Remap UVs and persist the changes 0 Answers