- Home /
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 :)
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.
Your answer
Follow this Question
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