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 () {
}
}
I wrote it to rename an instantiated prefab but this is not working.Why?And how can i correct it?
@UnityCoder, yaar it tried doing x++ ,but neither is the name changed nor are 7 instances created. @angrypenguin I get it but how do we change GameObject name?Thanks in advanced.
Here is your code modified by me and here its work fine for me
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++){
Object pathRef;
pathRef=Instantiate(PathFloorUnit,new Vector3(0,0,10*x),Quaternion.identity);
pathRef.name=“newName”+x;
}
}
// Update is called once per frame
void Update () {
@UnityCoder It doesnt work in my case.Object name is still original object(clone)and only one object forms.at least without referencing the prefab to object the 7 prefab instance were created as intended.Why is it so?Can it be a bug?Where do I find the answer?In answer.unity moderator 's’nt approved the question yet.Please help…
@SteveJ
When I didnt create a Path Ref object, the instantiate static function created 7 original prefab instances due to the for loop.But now only one instance of PathFloorUnit is forming.and is not being renamed though I renamed it in script.
The code definitely works fine - it must be your implementation. You should be attaching that script to an object (maybe an empty GameObject) and then assigning your prefab (that you want to instantiate) to PathFloorUnit in the Inspector.
The script is attached to an empty gameobject and I overwrote an existing copy of same name in project manager.It is still same as before. Though when I checked the inspector after selecting the CreateNew.cs, It shows a previously saved version of the script.overwrite doesnt change that.how do i correct it
In your project view, double-click CreateNew.cs to open it in your editor. Paste the modified code from the posts above over your existing code. Save and close the file.
Back in Unity, select your empty GameObject and take a look at the Inspector. You should see the spot where you can drag your prefab over from your Project contents.
@SteveJ Got it!I did as you have just told,then I RESET the script and it is working.Thanks.
I have been thinking ,Do experienced programmers too have to do all this debugging?