- Home /
dereference from a raycast
Im instantiating objects into the scene where the actual gameObject itself is a property of that object.
For example the actual gameobject is defined as building.buildingModel
I need a way to select this entire object on a mouse click
however when I use a raycast to select the object I can only access the gameObject through hit.collider.gameObject.
I need a way to dereference the gameObject to find the Object class (Building Class) its associated with.
Any Ideas?
So your building class is a component on that gameObject? If so then just use gameObject.GetComponent(ClassName);
Answer by michael 4 · May 02, 2011 at 11:31 PM
No, the gameobject is a componant of the class
but I figured out a nifty way to accomplish this
Start by creating an ArrayList that can hold your instances in real time
public ArrayList instancedBuildings = new ArrayList(); //this is used to dereferance my instances
Next create an int that can be used as a counter
public int objectNameIndex = 0;
now when you create an instance of the class you add that instance to the array
initialObject = new Building();
instancedBuildings.Add(initialObject);
now when I instantiate the physical model into the scene I append the counter to the end of the gameobject name so that each gameobject has a unique name (even if object being added are the same type, which by default gives them the same name)
initialObject.buildingModel = Instantiate(buildingPallete[index].buildingModel, Vector3.zero, Quaternion.identity) as GameObject;
initialObject.buildingModel.name = initialObject.buildingModel.name + System.Convert.ToString(objectNameIndex);
objectNameIndex++;
Now when Im ready to select the object I can cast a ray, detect the gameobject and then dereferance to find the class instance that the gameobject is part of.
//now cast a ray and make sure it hits a movableObject var ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; //var layerMask = 8; //set this to all layers a movableObject might be on Physics.Raycast(ray, out hit, 1000);
//did we hit movableObject?
if(hit.collider.tag == "movableObject" )
{
//if everything checks out then pass this gameobject to the selection
//CurrentlySelectedObject(hit.rigidbody.gameObject);
foreach(Building c in instancedBuildings)
{
if(hit.collider.gameObject.name == c.buildingModel.name)
{
CurrentlySelectedObject(c);
}
}
}
In this case the variable c contains the class instance Im looking for. You have to be careful though and make sure you continue to update your ArrayList properly if you start deleting instances from the scene. But other then that This lets you access the Custom Object class that was instantiated into the scene just by clicking the gameObject that is a member of that class. (This is assuming that your class has a gameObject defined in it)
I hope this might help anyone that runs into the same problem
Edit: If you plan on selecting your object so that you can delete the instance, use a for loop instead of foreach or your get a "Collection was modified; enumeration operation may not execute" Error
Your answer
Follow this Question
Related Questions
Is object at least partly visible? 1 Answer
Object Interaction 1 Answer
Android touch 3d Object event 1 Answer
Child instantiated object to RayCastHit's parent. 1 Answer
Interaction with various objects 0 Answers