我有这个脚本,它将在运行时添加SelectMove脚本,当用户触摸某个角色时。删除以前添加的游戏对象的组件
public GameObject target;
void Start() {
}
// Update is called once per frame
void Update() {
if(Input.touchCount > 0 || Input.GetTouch(0).phase == TouchPhase.Began){
ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
Debug.DrawRay(ray.origin,ray.direction * 20,Color.red);
if(Physics.Raycast(ray, out hit,Mathf.Infinity)){
Debug.Log(hit.transform.gameObject.name);
target = hit.transform.gameObject;
//Destroy(hit.transform.gameObject);
selectedPlayer();
}
}
}
void selectedPlayer(){
target.AddComponent(Type.GetType("SelectMove"));
}
在上面的代码中,如果用户点击玩家A,玩家A将使用加速度计移动。我需要做的是如果我点击另一个角色,比如玩家B,我需要玩家A停止移动,现在是玩家B的时间移动。但我似乎没有得到我想要的东西。我尝试用Destroy(this)
或通过这段代码破坏脚本:
if (target != null)
{
var sphereMesh = target.GetComponent<SelectMove>();
Destroy(sphereMesh);
}
但它仍然没有工作。
如果我不摧毁先前添加的脚本,如果用户点击另一个角色,则上一个选定的玩家仍会与新角色一起移动。
有没有另外一种方法可以实现我需要做的事情?
最好将它设置为false,而不是销毁'sphereMesh.enabled = false' –
销毁应该正常工作。 **(ONE)**在你的代码销毁(sphereMesh);添加Debug.Log(“我在这里”);确定是否实际上正在调用“Destroy”。回报给我们! – Fattie
它现在正在工作。我有与上述相同的代码,但它现在工作正常。我不知道我做了什么,但是谢谢! :) – kmf