- Home /
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.
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);
Note parent/child relationships are only between game objects, not other kinds of objects.
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. :)
Yep - we in the community call it Unity Script for a good reason - it is very unlike real Javascript.
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).
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…
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 (:
Unfortunately that gives me a NullReferenceException. I'm actually trying to go further up the tree than just the GameObject. But thanks :)