- Home /
Instantiated prefab name change
I wrote this code to change name of my instantiated prefabs that are supposed to be floor of my running game(I will worry about random instantiation later).The name simply does'nt change.Why is it so and how can I do it correctly?
using UnityEngine; using System.Collections;
public class CreateNew : MonoBehaviour {
public Object PathFloorUnit;
// Use this for initialization
void Start () {
for(int x=0;x<=6;x=+1){
Object pathRef;
pathRef=Instantiate(PathFloorUnit,new Vector3(0,0,10*x),Quaternion.identity);
pathRef.name="newName";
}
}
// Update is called once per frame
void Update () {
}
}
Answer by gamerguy · Oct 19, 2013 at 05:48 AM
I found the answer.x=+1 is wrong.it assigns +1 to x,so loop is not working.It should be x++ or x+=1 instead.Other than that its correct.TO BE NOTED that I corrected it & saved the changes and still it didnt work because the changes were not updated.So,after making changes sometimes we need to reset the script from the inspector...
Answer by meat5000 · Oct 17, 2013 at 03:02 PM
This works fine for me when I use a GameObject. I've not tried it on an Object.
I declared my variable (pathRef) outside all functions.
Answer by sethuraj · Oct 17, 2013 at 06:04 PM
try accessing/instantating Transform instead of GameObject.worked for me..
//C# Code
Transform pathRef=(Transform)Instantiate(PathFloorUnit,new Vector3(0,0,10*x),Quaternion.identity);
pathRef.name="newName";