猿问

单击实例化对象并返回它的编号 C#(UNITY)

我实际上不知道在 google 上搜索什么,所以我直接到了这里,对此我很抱歉。我的问题是我有这样的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);

  }

}

的输出将是


spr_boards 1


spr_boards 2


spr_boards 3


spr_boards 4


spr_boards 5


顺便说一下,我在另一个脚本上有一个点击事件


public void InstantiateTheObject(){

      fromAnotherScript.InstantiateObject();

}

然后将其拖到InstantiateTheObject检查器上的按钮上


现在我想单击此对象之一并返回它们的编号,但我不知道该怎么做。


编辑:


更多信息


例如,如果我单击实例化spr_board 1,则必须记录例如“这是对象 1”;


然后,如果我单击spr_board 2例如这必须记录例如“这是对象 2`;


我试过这个


public void InstantiateTheObject(){

   Debug.Log("objects number is : " + lobby_object_no);

      fromAnotherScript.InstantiateObject();

}

但问题是它总是得到我 last 的最后一个值。这不取决于我单击实例化对象的内容。


心有法竹
浏览 217回答 1
1回答

长风秋雁

如何从转换名称中获取数字:RaycastHit hit;&nbsp;void Update()&nbsp;{&nbsp; &nbsp; if (Input.GetMouseButtonDown (0))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);&nbsp; &nbsp; &nbsp; &nbsp;if(Physics.Raycast(ray, out hit))&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GameObject ourObject = hit.collider.gameObject;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string prefix = "spr_boards ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string txtNum = ourObject.transform.name.Remove(0, prefix.Length);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int number = Int32.Parse(txtNum);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.Log ("this is object " + number);&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp;}int number 是从 1 到 5 的数字
随时随地看视频慕课网APP
我要回答