- Home /
Parenting an instantiated prefab.
Hello, I need help with instantiating a prefab and straight away parenting it to the HeadBone. Here is my current script, if it may help. Thanks in advance if anyone can help! :)
var HairNumber : int = 0;
var HairArray : Transform[];
var HairBone : Transform;
function Update(){
if(HairNumber < 0){
HairNumber = 1;
}
if(HairNumber > 1){
HairNumber = 0;
}
}
function OnGUI(){
if(GUI.Button(Rect(10,450,140,140),"")){
Hair();
}
}
function Hair(){
HairNumber +=1;
if(HairNumber == 1){
Instantiate(HairArray[0], HairBone.position, HairBone.rotation);
//This is where I want to parent it to the HeadBone.
}
}
var go : GameObject = Instantiate(...) as GameObject;
go.transform.parent = HeadBone;
that's really all there is to it, not trying to trick you or anything
Answer by paulaceccon · Mar 31, 2013 at 08:22 PM
You could do:
function Hair()
{
HairNumber +=1;
if(HairNumber == 1){
var myGo : GameObject;
myGo = Instantiate(HairArray[0], HairBone.position, HairBone.rotation) as GameObject;
myGo.transform.parent = goFather.transform;
}
}
Just replace the goFather with the father game object.
When I do this, I get an error.
'Assets/CharacterCreate/Scripts/CharacterCreate.js(695,11): UCE0001: ';' expected. Insert a semicolon at the end.'
Please help. :(
This mean that you forgot a semicolon. I did a copy and paste here and couldn't find any missing ';'
This is what you have??
var HairNumber : int = 0;
var HairArray : Transform[];
var HairBone : Transform;
function Update(){
if(HairNumber < 0){
HairNumber = 1;
}
if(HairNumber > 1){
HairNumber = 0;
}
}
function OnGUI(){
if(GUI.Button(Rect(10,450,140,140),"")){
Hair();
}
}
function Hair()
{
HairNumber +=1;
if(HairNumber == 1){
GameObject myGO = Instantiate(HairArray[0], HairBone.position, HairBone.rotation) as GameObject;
myGo.transform.parent = goFather.transform;
}
}
Even though there is no missing ';' it still says there is on this line :(
GameObject myGO = Instantiate(HairArray[0], HairBone.position, HairBone.rotation) as GameObject;
Sorryyyy... $$anonymous$$y bad... I was thinking in C# and you are using UnityScript...
I've updated it.