Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by z7x9r0 · Jan 22, 2015 at 03:37 AM · javascriptinstantiatetouchguitexture

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image z7x9r0 · Jan 22, 2015 at 04:56 AM 0
Share

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.
         }
     }
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Wiki

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);
          }
      }
  }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges