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 falconfetus8 · Feb 18, 2013 at 11:55 PM · gameobjectprefabobjectname

How can I tell what prefab a GameObject belongs to?

I'm making a game where the player can interact with objects, but I need to be able to determine what object the player interacted with so that I don't call a script that the object doesn't have. For example, here is my player's script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControls : MonoBehaviour {
     
     void Start () {
     
     }
     
     void Update () {
         
         //Create a ray to see where the player is selecting
         Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
         Debug.DrawRay(ray.origin, ray.direction, Color.yellow);
         RaycastHit selectedObject;
         
         //Check to see if the player interacts with something
         if (Input.GetButtonDown("Interact")){
             
             if (Physics.Raycast(ray, out selectedObject)){
                 InteractWith(selectedObject.transform);
             }
                 
         }
     
     }
     
     private void InteractWith(Transform selectedObject){
         
         switch(selectedObject.name){
         
             case "Chest":
                 ChestBehavior script = selectedObject.GetComponent<ChestBehavior>();
                 script.OnInteract();
             break;
             
             case "Some other object":
                 //Call some other script
             break;
             
             case "etc.":
                 //etc.
             break;
             
         }
         
     }
 }

Notice the part that says "switch(selectedObject.name)"? On a small scale, this works just fine, but in a bigger project, I'm going to want to place multiple "Chest" objects in one room, and if they all have the same name it will be hard to know which chest is which in the hierarchy. Instead of testing for selectedObject.name, is there a way to test for something like selectedObject.prefabName?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Professor Snake · Feb 19, 2013 at 12:03 AM

You can simply check whether an object has a script by the value GetComponent returns. For instance: GetComponent("IDoNotExist") would return false, assuming a script by the name of IDoNotExist is not attached to the object. Alternatively, you could use another method: In addition to the scripts that handle the interactions in the objects, you could have a script attached to all of them called ActionScript. ActionScript's code would be a simplistic

 var functionToCall:String;
 function Activate(){
 transform.root.BroadcastMessage(functionToCall);
 }

Where functionToCall would be a reference to the name of the function you want to call in each object. Then, instead of a switch, you would just call

 selectedObject.GetComponent("ActionScript").Activate();

(Alternatively, you could use a similar method to get the name of the script, or even the type of the object in each interactible gameobject and use that to determine what you want to do)

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
avatar image
0

Answer by falconfetus8 · Feb 19, 2013 at 01:30 AM

Let me see if I'm understanding. In my case, I'd put OnInteract in place of functionToCall, and this would call the OnInteract function in every script that is attached to that GameObject?

Comment
Add comment · Show 2 · 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 AlucardJay · Feb 19, 2013 at 01:30 AM 1
Share

Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Here at Unity Answers, Answer means Solution, not Response.

avatar image Professor Snake · Feb 19, 2013 at 01:33 AM 0
Share

Indeed, in every script that has that function, and i am pretty sure there is a version of that function that calls the function in the gameobject plus its children. But that is only one of the methods. The general idea is to use a script shared on all objects to deter$$anonymous$$e what is what, ins$$anonymous$$d of names.

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

11 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

Related Questions

Get object/prefab name as string. 1 Answer

How to instantiate within GameObject 1 Answer

Get the name of an instance's prefab at runtime? 0 Answers

Get object name from raycast. 1 Answer

How to Reference Player when Instantiating Prefab 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