- Home /
How Do We Script a GameObject as a Child?
How do we make a prefab (var circle : GameObject;) into a child of the player in script? I can't seem to get it right. I just need to see an example in Javascript to drop into the script below.
REASON: To show the beginning touch on touchscreens with a circle. Seems simple, useful, and wanted to share.
What I have wrote so far:
#pragma strict
var circle : GameObject;
var fTC : Vector2;
function Update () {
if(Input.touchCount == 1){ //Does finger count on screen equal 1?
if(Input.GetTouch(0).phase == TouchPhase.Began){ //When touch on the touch screen begins.
fTC = Input.GetTouch(0).position;
Instantiate(circle, Camera.main.ScreenToWorldPoint(fTC), Quaternion.identity); //This needs to be a child of the Player in the scene.
Debug.Log(fTC);
}
if(Input.GetTouch(0).phase == TouchPhase.Ended){ //When touch on the touch screen ends.
//Code to get rid of circle/GameObject. I don't know how to successfully do this yet.
}
}
}
Also I watched some new Unity 4.6 UI videos, and sadly starting to learn more about using GUI Textures instead of sprites (what I am doing at the moment is using sprites). Is there a really big difference? Is it better to use a GUI Texture over a sprite in this case? Why or why not?
Saw this online: http://answers.unity3d.com/questions/12301/how-can-i-get-a-parent-gameobject-of-gameobject-us.html
Tried it below, I believe, but gives an error. Going to keep searching and trying in the meantime.
#pragma strict
var player : GameObject;
var circle : GameObject;
var fTC : Vector2;
//function Start(){
//player = GameObject.Find("Player");
//}
function Update () {
if(Input.touchCount == 1){ //Does finger count on screen equal 1?
if(Input.GetTouch(0).phase == TouchPhase.Began){ //When touch on the touch screen begins.
fTC = Input.GetTouch(0).position;
circle.transform.parent.player;
Instantiate(circle, Camera.main.ScreenToWorldPoint(fTC), Quaternion.identity); //This needs to be a child of the Player in the scene.
Debug.Log(fTC);
}
if(Input.GetTouch(0).phase == TouchPhase.Ended){ //When touch on the touch screen ends.
//Code to get rid of circle/GameObject.
}
}
}
Answer by IamAcoconut · Jan 22, 2015 at 11:54 AM
Try this:
#pragma strict
var player : GameObject;
var circle : GameObject;
var fTC : Vector2;
var circleclone: GameObject;//don't fill in the slot in the editor
//function Start(){
//player = GameObject.Find("Player");
//}
function Update () {
if(Input.touchCount == 1){ //Does finger count on screen equal 1?
if(Input.GetTouch(0).phase == TouchPhase.Began){ //When touch on the touch screen begins.
fTC = Input.GetTouch(0).position;
circle.transform.parent.player;
circleclone = Instantiate(circle, Camera.main.ScreenToWorldPoint(fTC), Quaternion.identity); //This needs to be a child of the Player in the scene.
circleclone.transform.parent = player.transform;//makes the parent the player
Debug.Log(fTC);
}
if(Input.GetTouch(0).phase == TouchPhase.Ended){ //When touch on the touch screen ends.
//Code to get rid of circle/GameObject.
Destroy(circleclone);
}
}
}
Answer by HarshadK · Jan 22, 2015 at 05:28 AM
All you need is one Transform.parent
Check the code below:
#pragma strict
var player : GameObject;
var circle : GameObject;
var fTC : Vector2;
//function Start(){
//player = GameObject.Find("Player");
//}
function Update () {
if(Input.touchCount == 1){ //Does finger count on screen equal 1?
if(Input.GetTouch(0).phase == TouchPhase.Began){ //When touch on the touch screen begins.
fTC = Input.GetTouch(0).position;
circle.transform.parent.player;
GameObject circleClone = Instantiate(circle, Camera.main.ScreenToWorldPoint(fTC), Quaternion.identity); //This needs to be a child of the Player in the scene.
Debug.Log(fTC);
// This line makes the player gameobject the parent of instantiated circleClone game object.
circleClone.transform.parent = player.transform;
}
if(Input.GetTouch(0).phase == TouchPhase.Ended){ //When touch on the touch screen ends.
//Code to get rid of circle/GameObject.
}
}
}
Each GUITexture requires one drawcall but you can actually reduce the drawcalls by using sprites like it is done by SpriteManager which packs sprites to reduce draw calls. But since the new GUi 4.6 does this job of packing sprites to reduce draw calls for you, why not use it? Plus it has various great features that you can use with few clicks.
Your answer
Follow this Question
Related Questions
Disable gui texture in a different way? 0 Answers
Calling Function in other Script via Touch => iOS Crash 1 Answer
"The name does not exist in the current context" error 1 Answer
Touch gui texture buggy 0 Answers
touch position 2 Answers