![Creative The name of the picture]()
Click Instantiated Object and Return What it's number C#(UNITY) [duplicate]
This question already has an answer here:
I don't know actually what to search on google so I directly get here and I am sorry about that . My problem is that I have Instantiate()
objects like this
Instantiate()
public void InstantiateObject(){
//the list has 5 objects in it .
for (int i = 0; i < list.Count; i++)
{
GameObject o = Instantiate(lobby_object) as GameObject;
o.transform.SetParent(pos_lobby_object);
o.transform.localScale = Vector3.one;
o.transform.name = "spr_boards " + lobby_object_no;
o.transform.localPosition = new Vector3(0, 0, 0);
NGUITools.SetActive(o, true);
}
}
The output of that would be
spr_boards 1
spr_boards 2
spr_boards 3
spr_boards 4
spr_boards 5
I have a click event by the way on another script
public void InstantiateTheObject(){
fromAnotherScript.InstantiateObject();
}
Then drag this InstantiateTheObject
to the button on the inspector
InstantiateTheObject
Now I want to click one of this object and return what number are they
but I don't know how to do it .
EDIT:
More information
If i clicked the Instantiated spr_board 1
for example this must log for example a "this is object 1";
spr_board 1
then if i click spr_board 2
for example this must log for example a "this is object 2`;
spr_board 2
I tried this
public void InstantiateTheObject(){
Debug.Log("objects number is : " + lobby_object_no);
fromAnotherScript.InstantiateObject();
}
but the problem is that it always get the last value of my last . It's not depending on what I click on the instantiated object.
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1,5,3,2,4
1 Answer
1
how to get number from transform name:
RaycastHit hit;
void Update()
{
if (Input.GetMouseButtonDown (0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
GameObject ourObject = hit.collider.gameObject;
string prefix = "spr_boards ";
string txtNum = ourObject.transform.name.Remove(0, prefix.Length);
int number = Int32.Parse(txtNum);
Debug.Log ("this is object " + number);
}
}
}
int number
is the number from 1 to 5
int number
Well, you can just find the space, and get the number after it from a string
– BugFinder
5 hours ago