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 bluepig · Dec 01, 2014 at 10:33 PM · gameobjectbuttonparenteventattach

How to get a parent object after clicking the childObject

Hi guys.I am a novice to unity. I tried to create a 2D top-down game and I came across a few problems; I want to attach a button to a block object, so I stored these two into an object called pairs. Each pairs object has two properties which are references to a block and a button. I want to create multiple pairs objects in random time and each time I click the button, it will disappear and the block will disappear after 10 seconds. Firstly, I tried to use OnGUI() to draw the button, but it seems to be very difficult since the position of the GUI button is not as the same in the world view and I could't figure out how to continuously add button in the OnGUI function since I don't know when a pairs object will be created. So I tried to make the button object a cube, and it's easy to bind a cube to block, and I can add textures to the button cube as button text.After clicking the button object, I want to destroy the block object attached to it, but I don't know how to get the block object.

I tried like this:Destroy(hit.collider.gameObject.transform.parent.GetChild(1),10F); It appears that I could't have access to these objects. but it looks like it didn't work.

So I am looking for help now.

Below are my codes: public class createObject : MonoBehaviour { List obstacles = new List(); List obstaclesPos = new List();

     void running (){
         Pairs zhu = new Pairs ("car");
         obstacles.Add (zhu);
         obstaclesPos.Add (zhu.blockPos);    
     }
 
     void Start() {
         InvokeRepeating ("running", 5F,5F);
     }
 
     void Update (){
         if(Input.GetMouseButtonDown(0)){
             RaycastHit hit;
             Ray ray =  Camera.main.ScreenPointToRay(Input.mousePosition);
             if(Physics.Raycast(ray, out hit)){
                 Destroy(hit.collider.gameObject,10F);
                 Destroy(hit.collider.gameObject.transform.parent.GetChild(1),20F);
             }
         }
     }
 }

and here is the pairs class: public class Pairs{ public GameObject block; public GameObject button; public Vector3 blockPos; public string typeName;

 public Pairs(string typeName){
     this.blockPos = new Vector3 (Random.Range(-5f, 5f), Random.Range(-5f, 5f), 1);

     this.button = GameObject.CreatePrimitive (PrimitiveType.Cube);
     this.button.transform.position = new Vector3 (this.blockPos.x + 2, this.blockPos.y, 1);
     this.typeName = typeName;

     switch (typeName) {
     case "car":
         this.block = GameObject.CreatePrimitive (PrimitiveType.Capsule);
         break;
     }
     this.block.transform.position = this.blockPos;
     UnityEngine.Object.Destroy(this.block.GetComponent<Collider>());    
 }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jenci1990 · Dec 01, 2014 at 10:43 PM

Try this:

 Destroy(hit.collider.gameObject.transform.parent.GetChild(1).gameObject,20F);
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Attach object to avatar bone 1 Answer

How can I attach a gameobject to another without changing its scale. 1 Answer

Linking two objects together 2 Answers

Pausing an animation in a parent game object? 4 Answers

Make rotation gameobjects equal to parent 1 Answer


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