Reputation: 11
So I instantiated an Object but i want to change some of its properties right after instantiating it. So i have to do something like:
Gameobject instantiated.GetComponent<>().property = "example Value";
Here is my code so far:
public void createObject(GameObject obj)
{
//Debug.Log(pos + i*offset);
Instantiate(obj, pos + i*offset, obj.transform.rotation).transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, false);
i++;
}
if i try to do something like: Gameobject instantiated = Instantiate(obj, pos + i*offset, obj.transform.rotation).transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, false);
it says: "void" cant be converted to "UnityEngine.GameObject".
Are there other ways to do it? Thx for every helpful advice
Upvotes: 0
Views: 445
Reputation: 31
You need to cache it first, then you can apply any proper changes to it. Like this:
GameObject newObject = Instantiate(YourPrefab);
newObject.someProp = something;
So in your case, do this:
public void createObject(GameObject obj)
{
GameObject newObject = Instantiate(obj, pos + i*offset, obj.transform.rotation);
newObject.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, false);
i++;
}
Upvotes: 2