-3

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

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

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";

then if i click spr_board 2 for example this must log for example a "this is object 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.

Community
  • 1
  • 1
Ketskie
  • 15
  • 7
  • Well, you can just find the space, and get the number after it from a string – BugFinder Jul 24 '18 at 09:37
  • 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 Jul 24 '18 at 09:46
  • 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 Jul 24 '18 at 09:48
  • 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 Jul 24 '18 at 09:50
  • I'm struggling on having an exact output of each object for example i click the first instantiated object then it must be `spr_boards 1` then logs a "this is object 1"; and so on – Ketskie Jul 24 '18 at 09:53
  • but we explained how to get the number, so again, what specifically are you struggling with – BugFinder Jul 24 '18 at 09:54
  • I edited my question sir @BugFinder – Ketskie Jul 24 '18 at 09:58
  • Ok so this has nothing to do with click or anything.. While its not clear in your code where lobby_object_no comes from, Im going to guess that it says 5 because thats the last value assigned to it, not the one from the object, hence, we have explained how to get the number from the name of the object (PS I am NOT male) – BugFinder Jul 24 '18 at 10:01
  • Attach the `MeshDetector` from the duplicate to the prefab "lobby_object" GameObject. When instantiated, and clicked on, it should how you the name. I assume you're using a 3D Object so see the **#6** from the duplicate If you're using something else then check the rest of the answer from the duplicate – Programmer Jul 24 '18 at 10:03
  • 1
    Wow thank you programmer – Ketskie Jul 24 '18 at 10:08
  • You're welcome! – Programmer Jul 24 '18 at 11:40

1 Answers1

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

Jinjinov
  • 2,554
  • 4
  • 26
  • 45