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 danndx · Aug 18, 2013 at 01:55 PM · raycastprefablistsfunctionsnames

Create game objects with unique IDs and detect them?

Hey There,

Problem: The player in this scene has multiple different types of objects in their inventory. Using keys 1 to 4, they can be placed down onto a (3D) grid, and when looking at them I'd like to display what type of object they are looking at, and what it's name or ID is. For example, using a base table prefab I place down three tables. These tables are automatically named or ID'd as table1, table2 and table3.

My Q: How can I have the table prefab dynamically named each time one is placed down or removed? And using ray casting from the player location in the forward direction, how can I then detect this unique-ness between the objects? (It seems difficult trying to get the raycast to detect between all these different objects and then their types)

Possible Pseudocode:

 update function
 {
     list = list of existing object types;
     listTable = list of existing tables only;
 
     if (key pressed "create table")
     {
         At 3D grid location were looking at, create table;
         Name the table based off length of list + 1;
     }
         
     if (Physics Raycast)
     {
         if (ray hits object type "table")
         {
             display name or ID;
         }
     }
 }

Cheers :)

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

1 Reply

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

Answer by ArkaneX · Aug 18, 2013 at 08:02 PM

To assign unique id to object instantiated from prefab, you can use static global ID stored in the prefab. When you create new instance, you have to get current global ID, use it to create instance ID, and then increment it. Something like this:

 public class SphereScript : MonoBehaviour
 {
     private static int _globalId = 1;
 
     private string _id;
 
     public string Id
     {
         get { return _id; }
     }
 
     void Awake()
     {
         _id = String.Concat("Sphere", _globalId++);
     }
 }

Unity UI runs in one thread, so reading from _globalId and incrementing it will always result in unique value. The above code handles creating new ID when instantiating, but if you need to free ID when object is destroyed and then reuse it in another object, then you would need another approach, involving storing and managing all IDs currently in use.

To create a prefab instance on the table and then detect it and display its ID in a GUIText, you have to create another script and attach it to your character:

 public class CharScript : MonoBehaviour {
     
     public Transform prefab;
     public GUIText infoText;
 
     private Transform _characterCamera;
 
     void Awake()
     {
         _characterCamera = transform.FindChild("Main Camera");
     }
 
     void Update ()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             HandleClick();
         }
 
         RaycastHit hit;
         string sphereId;
         if (Physics.Raycast(_characterCamera.position, _characterCamera.rotation * new Vector3(0, 0, 1), out hit) && hit.collider.tag == "Sphere")
         {
             var sphereScript = hit.collider.GetComponent<SphereScript>();
             sphereId = sphereScript.Id;
         }
         else
         {
             sphereId = "----";
         }
         infoText.text = String.Concat("ID: ", sphereId);
     }
     
     private void HandleClick()
     {
         RaycastHit hit;
         if (Physics.Raycast(_characterCamera.position, _characterCamera.rotation * new Vector3(0, 0, 1), out hit) && hit.collider.tag == "Table")
         {
             Instantiate(prefab, hit.point, Quaternion.identity);
         }
     }
 }

The above script was written for First Person Controller from standard Unity assets. It is not optimal of course, and requires polishing, but I believe explains how to achieve your goal.

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

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

Related Questions

Instantiate prefab from original position to click mouse point position 0 Answers

Instantiating prefab question. 1 Answer

How do i scale a prefab laser to hit.distance 1 Answer

How to effect and apply a common material to children individually... 2 Answers

Throw stuff! Instantiate preFab from game object to raycast point, at least I'd like to. 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