Hi.
I want to instantiate a prefab and save the modified new prefab object as an atribute of another Gameobject to access it later.
But if I access i get only the value of the origin prefab and dont the changed values.
Why is it so?
Her some code:
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
public class LevelDungeon1 : MonoBehaviour {
public GameObject prefab;
private GameObject childObject ;
private List<GameObject> list;
void Start () {
list= new List<GameObject>();
//start
//change prefab attributes with methods
//..... prefab.GetComponent<Script1>().method1();
//..... prefab.GetComponent<Script1>().method2();
childObject = Instantiate(prefab) as GameObject;
list.Add(prefab); //Here I want to save the modified prefab in a list. so i can later access the modfied values.
childObject.transform.parent = this.gameObject.transform;
void Update () {
}
}
In the script of the prefab i change for example:
void Start() {
this.name="newName";
}
but if i check the value it is the origin prefab name, originName …
I guess you’re mixing up prefabs and instances.
Instantiate creates a new gameobject (the instance) from the prefab (the template). It does not change the prefab itself, nor it does create a new prefab.
ok! I created a level complete level with this script above. The level parts are in another script. There i save start and exit gameobject. the Vector2 is important for me i want to access later. The values are right i see it at runtime in the inspector but now i want to access these attributes (start/exit) of the levelpart. but if i want Gameobject.Find(“part1”)… there comes NULL. in the Start method. i want to access the level part what i see in the inspector there are the correct values.
What is my mistake?
My idea:
-create a whole level in one script
-create levelparts with another script
-access start and exit to connect the parts like a puzzle
and I cant access the Gameobject start, exit i got the prefab values or the last created one.
How is it possible to create an instance of an prefab an save the created gamobject reference in an attribute?
// the name on the prefab template, doesn't change as the script attached to it isn't
//"running" because there isn't a gameobject for the script component to be attached to
prefab.name;
// the name of the instance created by using the prefab and instantiate
// name changes once Start has run
childObject.name;