- Home /
Creates object under itself, not parent
I'm trying to instantiate an object to be the child of the parent of my button, but it is created as the child of the button. Can anyone help? I adapted this from the GUI.Button script and my prefab is a .blend file.
// Draws 2 buttons, one with an image, and other with a text
// And print a message when they got clicked.
var pic : boolean;
var btnTexture : Texture;
var btnText = "Click";
var btnX : int;
var btnY : int;
var btnSizeX: int;
var btnSizeY: int;
var sword: GameObject;
private var Player : GameObject;
Player = GameObject.FindWithTag('Player');
private var listLength = 0;
var swordList = new GameObject[listLength];
function execute(){
var P =transform.Find('Link').Instantiate(sword, transform.position, Quaternion.identity);
P.transform.parent = transform;
listLength++;
//swordList.Add(P);
sword.tag = 'Sword';
// Create a set of ifs and elifs to check what direction Link is facing
// figure out how to change the pos of
// the created sword, not the prefab
P.transform.localPosition.x += 2;
// Spin the object around the world origin at 20 degrees/second.
transform.RotateAround(transform.position, Vector3.zero, 20*Time.deltaTime);
// Destroys the swords created after one second of creation, in order of creation
for(i = 0; i < swordList.Length; i++){
Destroy(swordList[i].gameObject, 5);
listLength--;
}
}
function OnGUI() {
if (!btnTexture) {
Debug.LogError("Please assign a texture on the inspector");
return;
}
if(pic){
if (GUI.Button(Rect(btnX,btnY,btnSizeX,btnSizeY),btnTexture)){
Debug.Log("Clicked the button with an image");
execute();
}
}
else{
if (GUI.Button(Rect(btnX,btnY,btnSizeX,btnSizeY),btnText)){
Debug.Log("Clicked the button with text");
execute();
}
}
}
Slightly confused: you say "I'm trying to instantiate an object to be the child of the parent of my button" however all I see in the code is that you draw one of two buttons, depending on whether 'pic' is true or false.
Your code doesn't seem to match your question, so unsure how to respond...
Assu$$anonymous$$g you want the parent of the object this script is attached to, change line 19 to:
P.transform.parent = transform.parent;
Your answer
Follow this Question
Related Questions
[SOLVED] Only instantiating once 1 Answer
How do you only Instantiate only once when pressing a button once? 2 Answers
How to activate a button? 1 Answer
Instantiate problem in a RTS game 0 Answers