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 vaeylon · Apr 25, 2013 at 11:58 AM · gameobjectparentclass

Accessing a GameObject parent Object

Hello there, quite new with Unity but experienced in Javascript :)

I have a question that I can't seem to answer by myself so I come to you in hope to get one :)

I am creating a Tile-based game with much informations attached to each tiles, so I created a Tile class with my properties into it, and in those properties there is a sprite property which is containing a GameObject ( and its meshes, colliders, etc… ). I use raycasting to click on my tiles and get back which one was clicked, but i want to access the Tile object with all the properties attached. I can't seem to get to it. I tried

hit.collider.gameObject.transform.root

or

hit.transform.root

( and also with parent instead of root ) and it always seem to stop to the Plane primitive not going back further to the sprite property and the Tile object.

Here is my code so you can understand :

 class Tile extends System.Object{
     var sprite : GameObject = GameObject.CreatePrimitive(PrimitiveType.Plane);
     var type : String = "Tile" ;
     var allegiance : String = "Neutral" ;
     
     function Tile(xc:int, zc:int, ox:int, oz: int){
         this.sprite.transform.localScale.x *= .1 ;
         this.sprite.transform.localScale.z *= .1 ;
         this.sprite.transform.position = Vector3(ox + xc, 0, oz + zc);
         this.sprite.renderer.material.color = Color.white ;
         this.sprite.renderer.material.shader = Shader.Find("Self-Illumin/Diffuse");
     }
 }

     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if( Physics.Raycast(ray, hit, 5000.0) ){

         // This actually is the correct Tile.sprite gameObject selected here
         var select : GameObject = hit.collider.gameObject ; 

         select.transform.tag = "selected" ;
         originalSelectedObjectColor = select.renderer.material.color ;

         // This correctly colors my tile in red
         select.renderer.material.color = Color.red ;

         // This get me "Plane"
         Debug.Log(select.transform.root);

         if( select.GetType() == "Plane" ){
             createTower(aPlayers[0], hit.collider.transform.position.x, hit.collider.transform.position.z);
         }
     }


Any idea on how to do this ? Thanks a lot.

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 whydoidoit · Apr 25, 2013 at 02:43 PM 0
Share

Can you post a screen shot of your object hierarchy?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Apr 25, 2013 at 02:25 PM

The typical code I see handles what you are doing a bit differently. Your Tile class would be derived from Monobehaviour. Then you would add it as a component (GameObject.AddComponent()) to the game object you create. To get access to the tile data you would use GameObject.GetComponent().

There may be good reasons for you to dynamically create your tiles, but in the couple of tile/card games I've written, I use a tile prefab. That is I create a single tile with a tile script as a prefab and use Instantiate() to create the many instances of the tiles at runtime.

Now with the Tile component attached to the game object, you would do something like:

 var tile = select.GetComponent(Tile);


Comment
Add comment · Show 6 · 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 robertbu · Apr 25, 2013 at 02:32 PM 0
Share

Note parent/child relationships are only between game objects, not other kinds of objects.

avatar image vaeylon · Apr 25, 2013 at 02:34 PM 0
Share

Oh! That must be why I can't go further up the tree in my case. I assumed JavaScript in Unity would work exactly as it would on the web, but I assumed wrongly. I get $$anonymous$$dfucked a bit more every day with the strange behaviours I encounter in Unity compared to what I'm used to. :)

avatar image whydoidoit · Apr 25, 2013 at 02:44 PM 0
Share

Yep - we in the community call it Unity Script for a good reason - it is very unlike real Javascript.

avatar image robertbu · Apr 25, 2013 at 02:44 PM 0
Share

It takes a bit of time to wrap your $$anonymous$$d around the component model of Unity. The way I describe above is the typical way I see tile games structured. Transforms are all about the physical relationship between game object and their parent (or the world if there is no parent).

avatar image vaeylon · Apr 25, 2013 at 02:46 PM 0
Share

I'll try to get your solution into my game but I'm not quite certain it would resolve my issue, as I still need to raycast on the tile and then get to the properties I had set…

Show more comments
avatar image
0

Answer by arn1471 · Apr 25, 2013 at 02:20 PM

Assuming i understand what your asking its really straight forward:

 select.transform.parent.gameObject

If there is something you cannot answer I suggest google be your first port of call for these simple matters (:

Comment
Add comment · Show 1 · 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 vaeylon · Apr 25, 2013 at 02:28 PM 0
Share

Unfortunately that gives me a NullReferenceException. I'm actually trying to go further up the tree than just the GameObject. But thanks :)

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

13 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

Related Questions

Class hierarchy, gameObject, Raycast 1 Answer

Pausing an animation in a parent game object? 4 Answers

GameObject Disappears when Setting Parent 1 Answer

How to get a parent object after clicking the childObject 1 Answer

change pivot of parent game object 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