- Home /
Cloned Prefab
I instantiate a prefab and it becomes a clone, for example:
prefab1 now becomes prefab1(Clone).
Unfortunately, I'm losing my prefab connection because of it:
var Hero : GameObject;
function Update () {
transform.position.x = Hero.position.x;
transform.position.y = Hero.position.y + 10;
}
How can I reference the cloned prefab instead of just the prefab itself? Any help is appreciated. Thank you.
Answer by CostelloNicho · Nov 29, 2012 at 04:22 AM
Try this: ( adapted from your comments )
var hero;
void Start(){
hero = GameObject.FindWithTag("Player"); //be sure to set the tag on the correct object
}
void Update(){
hero.transform.position.x =hero.transform.position.x;
hero.transform.position.y = hero.transform.position.y + 10;
}
thank you for the post. i get the error 'void is not a valid macro'
Sorry about that I'm a C# programmer. Happy I could help though
this worked for me! thx again friend!
var hero;
function Update(){ hero = GameObject.FindWithTag("Player"); //be sure to set the tag on the correct object transform.position.x = hero.transform.position.x; transform.position.y = hero.transform.position.y + 10; }
Answer by MountDoomTeam · Nov 29, 2012 at 01:46 AM
How can you lose the prefab connection? I think you only do that if you change the prefab contents? And it gives a warning. how to reference cloned prefab?
instantiating a GameObject in the scene is the same as duplicating it. you simply say
instantiate(instantiated GameObject, position, rotation);
just reference it the same as if you are going to do a rotation on it, except duplicate it,it's in the same script?
people prefer it when you write an actual question title because it's the academically proper educated way of corresponding in writing in public forums. writing clone prefabs is a bit like having a question called -clicking area- or -pressing button- it's too vague!
thanks for the feedback. i have a camera that is linked to a prefab. when the prefab is instantiated into the scene, it becomes "prefab(Clone)" and my camera stops following the player because it can't find the prefab. I need to be able to directly call the "prefab(Clone)" version ins$$anonymous$$d of the regular prefab prior to instantiating. do you know how to do that?
yes, normally you should link a camera to GameObject already on the scene. find with tag for everything unless you have already as a reference. I didn't answer that you have to findobject with tag because with 326 experience I thought you must be asking something much more complicated! I figured you wanted to duplicate a clone!
I accidentally made a prefab that instantiated itself within itself and it did
prefab(clone)
prefab(clone)(clone)
prefab(clone)(clone)(clone)
prefab(clone)(clone)(clone)(clone)
prefab(clone)(clone)(clone)(clone)(clone)
prefab(clone)(clone)(clone)(clone)(clone)(clone)
$$anonymous$$any hundreds of times:)
thanks but that's not what is happening to me. it's already fixed now. 326 experience doesn't mean i know complicated things. you have 359 and couldn't figure this question out! thx for the help though.
I figured I should help some people on this forum but I was too sleepy to understand the question! I read it when I wasn't drowsy and I realised I had miss read it!
still, it's important to write questions on the forum as questions and not as titles, it's more adult and practical.
i'll stop being such a child with my titles since it bothers the adults.
Answer by CostelloNicho · Nov 29, 2012 at 03:23 AM
I'm not sure I understand the problem fully, but I'm assuming your using GameObject.Find() to find whatever your camera is trying to follow, but after instantiating the object the name has "(clone)" at the end.
If this is the case you can do this:
Create a variable to track the instantiated object.
GameObject trackThisObject;
assign the instantiated object.
trackThisObject = GameObject.Instantiate( instantiated GameObject, pos, rot);
Use track this object for camera tracking.
Hope this helps, a bit lost by the question.
is it possible to have my camera follow an object with the tag "Player" ins$$anonymous$$d of trying to code for the (Clone)?
i get 'object reference is not set to an instance of the object'...how can i reference a cloned prefab directly by name or even tag?
Yes you can find an object by tag:
GameObject.FindWithTag("nameOfObject");
here's a link:
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindWithTag.html
yes but how do i actually add it to my code? i tried this: var hero = GameObject.Find("Player");
function Update () { hero.position.x = hero.position.x; hero.position.y = hero.position.y + 10; }