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.