Click Instantiated Object and Return What it's number C#(UNITY) [duplicate]

Multi tool use
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.
well you are making 5 identical things and placing them over the top of each others, so I guess the last one is on top
– BugFinder
5 hours ago
The order of the list i have actually is
1,5,3,2,4
so the bottom is always selected . So it is 4 always @BugFinder.– Ketskie
5 hours ago
1,5,3,2,4
what exactly then are you struggling with (despite the deleted comment) it seems your problem is you get a different object on clicking to what you expected
– BugFinder
5 hours ago
Wow thank you programmer
– Ketskie
4 hours ago
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
this won't do sorry . But thank you
– Ketskie
5 hours ago
why not? this is how you get a number from the name
– JinJi
5 hours ago
do you also need to get the object?
– JinJi
5 hours ago
They have designated numbers sir.
– Ketskie
4 hours ago
i have edited the answer - now it also outputs the debug log string as you requested
– JinJi
4 hours ago
Well, you can just find the space, and get the number after it from a string
– BugFinder
5 hours ago