按一下按钮无法正常工作 [英] Button click does not work correctly

查看:230
本文介绍了按一下按钮无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我在做什么错了,它是一个2D的项目有两个objects.One有一个 RigidBody2D BoxCollider2D 组件。第二个对象只有 BoxCollider2D 。和底部有一个按钮,当按下按钮 Object1 落在 Object2的摧毁再次实例化 Object1 。但是,当 Object1实例化然后点击按钮不起作用。和错误想出了这样的:



类型Rigidbody2D的目标已被摧毁,但你仍然在试图访问它。你的脚本应该要么检查它是否是null或者你不应该破坏对象



对象1:



在这里输入的形象描述



对象2:





按钮点击:





对象1脚本:

 公共类Object1:MonoBehaviour {

公共静态Object1实例;

[SerializeField]
私人Rigidbody2D体;

[SerializeField]
私人布尔hasdropped;

[SerializeField]
私人布尔点击;

[SerializeField]
私人浮动PointerPos;

[SerializeField]
私人浮动BorderX;

无效唤醒(){

如果(例如== NULL){

实例=这一点;
}

//进行屏幕
的Vector3 gameScreen = Camera.main.ScreenToWorldPoint的宽度(新的Vector3(Screen.width,Screen.height,0));

BorderX = gameScreen.x - 0.6f;




body.isKinematic = TRUE;
点击= TRUE;
hasdropped = TRUE;


}



无效FixedUpdate(){

如果(点击进入){

的Vector3 TEMP = transform.position;

PointerPos = Camera.main.ScreenToWorldPoint(Input.mousePosition).X;

temp.x = Mathf.Clamp(PointerPos,-BorderX,BorderX);

body.position =温度;

如果(hasdropped){
的回报;

}

}


}

公共无效ButtonClick(){

body.isKinematic = FALSE;

}


}

对象2脚本:

 公共类Object2的:MonoBehaviour {

[SerializeField]
私人游戏物体BallClone;




无效OnCollisionEnter2D(Collision2D目标){

摧毁(target.gameObject);

实例化(BallClone,新的Vector3(0F,2F,59F),Quaternion.identity);


}

}


解决方案

的问题是,你ClickButton引用的Object1Prefab(活对象,而不是预制)在点击()。 (但引用预制不会在所有的工作)



既然你破坏它,你错过了链接。 (检查一下:选择在层次结构中的ClickButton对象,有脚本参考,右键单击游戏按钮一次现在取消并选择层次再......就消失了ClickButton对象。)



尽量使听者或把ButtonClick()方法里面Object2的(你不破坏):

 使用UnityEngine; System.Collections中使用
;

公共类Object2的:MonoBehaviour {

[SerializeField]
私人游戏物体BallClone;

公共游戏物体矿;


无效OnCollisionEnter2D(Collision2D目标){

摧毁(target.gameObject);

矿=实例化(BallClone,新的Vector3(0F,2F,59F),Quaternion.identity)作为游戏对象;


}




公共无效ButtonClick(){
//这只是一个例子。你可能会想缓存此信息为更好的做法,而不是调用GetComponent所有的时间
mine.GetComponent< Rigidbody2D> ().isKinematic = FALSE;
}




}

1拖动GameObject1Prefab(活动对象)到挖掘领域的Object2的脚本。参考公共游戏物体矿



第一次点击你要毁掉它,并创建一个新的,编程再次引用上线新的:

 矿=实例化(BallClone,新的Vector3(0F,2F,59F),Quaternion.identity)作为游戏对象; 



在ClickButton对象,您拖动GameObject2,而不是1,对通过点击()。



这是清楚了吗?对不起,我的英语。 :〜


I don't know what i am doing wrong it's a 2D project there are two objects.One has a RigidBody2D and BoxCollider2D component. Second object only has BoxCollider2D. And bottom there is a button when press button Object1 fall on Object2 and Destroy and Instantiate Object1 again. But when Object1 Instantiate then click on button does not work. And error came up like this :

The object of type Rigidbody2D has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

Object 1:

Object 2:

Button Click:

Object 1 Script:

public class Object1 : MonoBehaviour {

    public static Object1 instance;

    [SerializeField]
    private Rigidbody2D body;

    [SerializeField]
    private bool hasdropped;

    [SerializeField]
    private bool click;

    [SerializeField]
    private float PointerPos;

    [SerializeField]
    private float BorderX;

    void Awake(){

        if (instance == null) {

            instance = this;
        }

        //take width of screen
        Vector3 gameScreen = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height,0));

        BorderX = gameScreen.x - 0.6f;




        body.isKinematic = true;
        click = true;
        hasdropped = true;


    }



    void FixedUpdate () {

        if (click) {

            Vector3 temp = transform.position;

            PointerPos = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;

            temp.x = Mathf.Clamp(PointerPos,-BorderX,BorderX);

            body.position = temp;

            if(hasdropped){
                return;

            }

        }


    }

    public void ButtonClick(){

        body.isKinematic = false;

    }


}

Object 2 Script:

public class Object2 : MonoBehaviour {

    [SerializeField]
    private GameObject BallClone;




    void OnCollisionEnter2D(Collision2D target){

        Destroy (target.gameObject);

        Instantiate (BallClone,new Vector3(0f,2f,59f),Quaternion.identity);


    }

}

解决方案

The problem is that you referenced the Object1Prefab (the live object, not the prefab) on ClickButton On Click(). (But referencing the prefab will not work at all)

Since you destroy it, you missed the link. (Check it: Select the ClickButton object on the hierarchy, there is the script reference, right? Click the game button once. Now deselect and select ClickButton object on hierarchy again... it is gone.)

Try to make a listener or to put the ButtonClick() method inside the object2 (that you don't destroy):

using UnityEngine;
using System.Collections;

public class Object2 : MonoBehaviour {

    [SerializeField]
    private GameObject BallClone;

    public GameObject mine;


    void OnCollisionEnter2D(Collision2D target){

        Destroy (target.gameObject);

        mine = Instantiate (BallClone,new Vector3(0f,2f,59f),Quaternion.identity) as GameObject;


    }




public void ButtonClick(){
//this is just an example. You might wanna to cache this info for better practice and not call GetComponent all the time
            mine.GetComponent<Rigidbody2D> ().isKinematic = false;
        }




}

1- Drag GameObject1Prefab (live object) to the Object2 script on "mine" field. Reference to public GameObject mine.

The first click you gonna destroy it, and create a new one and programmatically referencing the new one again on the line:

mine = Instantiate (BallClone,new Vector3(0f,2f,59f),Quaternion.identity) as GameObject;

On the ClickButton Object, you drag the GameObject2, not the 1, on the On Click().

Is this clear? Sorry my english. :~

这篇关于按一下按钮无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆