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 michael 4 · May 02, 2011 at 09:15 PM · raycastobject

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?

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 Antony-Blackett · May 02, 2011 at 09:29 PM 0
Share

So your building class is a component on that gameObject? If so then just use gameObject.GetComponent(ClassName);

1 Reply

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

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

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

No one has followed this question yet.

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


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