Boolean 引用非静态变量

Boolean 引用非静态变量,boolean,components,unity5,Boolean,Components,Unity5,我正在做一个基本的篮球游戏,我有一个叫做dunkComplete的公共布尔值,它在球被扣篮并附加到球脚本后被激活,我试图在游戏管理器脚本中引用这个布尔值,但出于某种原因,即使dunkComplete变为真,它的游戏管理器对应物却没有,这是游戏管理器脚本供参考 using System.Collections; using System.Collections.Generic; using UnityEngine; public class game_manager : MonoBehaviou

我正在做一个基本的篮球游戏,我有一个叫做dunkComplete的公共布尔值,它在球被扣篮并附加到球脚本后被激活,我试图在游戏管理器脚本中引用这个布尔值,但出于某种原因,即使dunkComplete变为真,它的游戏管理器对应物却没有,这是游戏管理器脚本供参考

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class game_manager : MonoBehaviour {

    public GameObject Basket;

    private float x_value;
    private float y_value;

    public GameObject ball;
    private ball_script basketBallScript;
    private bool dunkCompleteOperation;

    // Use this for initialization
    void Start () {
        basketBallScript = ball.GetComponent<ball_script>();

        Vector2 randomVector = new Vector2(Random.Range(-9f, 9f), Random.Range(0f, 3f));

        Debug.Log(randomVector);

        Instantiate(Basket, randomVector, transform.rotation);

        Instantiate(ball, new Vector2(0, -3.5f), transform.rotation);
    }

    // Update is called once per frame
    void Update () {

        dunkCompleteOperation = basketBallScript.dunkComplete;

        if (dunkCompleteOperation == true)
        {
            Vector2 randomVector = new Vector2(Random.Range(-9f, 9f), Random.Range(0f, 3f));

            Instantiate(Basket, randomVector, transform.rotation);
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类游戏经理:单一行为{
公共游戏对象篮;
私有浮动x_值;
私人浮动y_值;
公共游戏物体球;
私人篮球脚本;
私人布尔邓克完成运营;
//用于初始化
无效开始(){
basketBallScript=ball.GetComponent();
Vector2 randomVector=新的Vector2(随机范围(-9f,9f),随机范围(0f,3f));
Log(随机向量);
实例化(篮子、随机向量、变换、旋转);
实例化(球,新向量2(0,-3.5f),变换。旋转);
}
//每帧调用一次更新
无效更新(){
dunkCompleteOperation=basketBallScript.dunkComplete;
如果(dunkCompleteOperation==true)
{
Vector2 randomVector=新的Vector2(随机范围(-9f,9f),随机范围(0f,3f));
实例化(篮子、随机向量、变换、旋转);
}
}
}

非常感谢您的帮助。

您的basketBallScript没有引用您实例化的ball对象的ball\u脚本。您应该保留对您创建的游戏对象的引用,然后分配basketBallScript

试试这个:

public class game_manager : MonoBehaviour {

    public GameObject Basket;

    private float x_value;
    private float y_value;

    public GameObject ball;
    private ball_script basketBallScript;
    private bool dunkCompleteOperation;

    // Use this for initialization
    void Start () {

        Vector2 randomVector = new Vector2(Random.Range(-9f, 9f), Random.Range(0f, 3f));

        Debug.Log(randomVector);

        Instantiate(Basket, randomVector, transform.rotation);
        // Get the ref. to ballObj, which is instantiated. Then assign the script.
        GameObject ballObj = Instantiate(ball, new Vector2(0, -3.5f), 
        transform.rotation) as GameObject;
        basketBallScript = ballObj.GetComponent<ball_script>();
    }

    // Update is called once per frame
    void Update () {

        dunkCompleteOperation = basketBallScript.dunkComplete;

        if (dunkCompleteOperation == true)
        {
            Vector2 randomVector = new Vector2(Random.Range(-9f, 9f), Random.Range(0f, 3f));

            Instantiate(Basket, randomVector, transform.rotation);
        }
    }
}
公共类游戏管理器:单一行为{
公共游戏对象篮;
私有浮动x_值;
私人浮动y_值;
公共游戏物体球;
私人篮球脚本;
私人布尔邓克完成运营;
//用于初始化
无效开始(){
Vector2 randomVector=新的Vector2(随机范围(-9f,9f),随机范围(0f,3f));
Log(随机向量);
实例化(篮子、随机向量、变换、旋转);
//获取已实例化的ballObj的引用。然后分配脚本。
GameObject ballObj=实例化(球,新向量2(0,-3.5f),
旋转)作为游戏对象;
basketBallScript=ballObj.GetComponent();
}
//每帧调用一次更新
无效更新(){
dunkCompleteOperation=basketBallScript.dunkComplete;
如果(dunkCompleteOperation==true)
{
Vector2 randomVector=新的Vector2(随机范围(-9f,9f),随机范围(0f,3f));
实例化(篮子、随机向量、变换、旋转);
}
}
}

另外,为了使您的代码在将来更容易被其他人理解,您可能希望签出。

您的basketBallScript不引用您实例化的ball对象的ball_脚本。您应该保留对您创建的游戏对象的引用,然后分配basketBallScript

试试这个:

public class game_manager : MonoBehaviour {

    public GameObject Basket;

    private float x_value;
    private float y_value;

    public GameObject ball;
    private ball_script basketBallScript;
    private bool dunkCompleteOperation;

    // Use this for initialization
    void Start () {

        Vector2 randomVector = new Vector2(Random.Range(-9f, 9f), Random.Range(0f, 3f));

        Debug.Log(randomVector);

        Instantiate(Basket, randomVector, transform.rotation);
        // Get the ref. to ballObj, which is instantiated. Then assign the script.
        GameObject ballObj = Instantiate(ball, new Vector2(0, -3.5f), 
        transform.rotation) as GameObject;
        basketBallScript = ballObj.GetComponent<ball_script>();
    }

    // Update is called once per frame
    void Update () {

        dunkCompleteOperation = basketBallScript.dunkComplete;

        if (dunkCompleteOperation == true)
        {
            Vector2 randomVector = new Vector2(Random.Range(-9f, 9f), Random.Range(0f, 3f));

            Instantiate(Basket, randomVector, transform.rotation);
        }
    }
}
公共类游戏管理器:单一行为{
公共游戏对象篮;
私有浮动x_值;
私人浮动y_值;
公共游戏物体球;
私人篮球脚本;
私人布尔邓克完成运营;
//用于初始化
无效开始(){
Vector2 randomVector=新的Vector2(随机范围(-9f,9f),随机范围(0f,3f));
Log(随机向量);
实例化(篮子、随机向量、变换、旋转);
//获取已实例化的ballObj的引用。然后分配脚本。
GameObject ballObj=实例化(球,新向量2(0,-3.5f),
旋转)作为游戏对象;
basketBallScript=ballObj.GetComponent();
}
//每帧调用一次更新
无效更新(){
dunkCompleteOperation=basketBallScript.dunkComplete;
如果(dunkCompleteOperation==true)
{
Vector2 randomVector=新的Vector2(随机范围(-9f,9f),随机范围(0f,3f));
实例化(篮子、随机向量、变换、旋转);
}
}
}

另外,为了使您的代码在将来更容易被其他人理解,您可能需要查看。

您可以分享您的ball\u脚本吗?还有,是只有一个球,还是你在灌篮或类似的事情后产生了另一个球?你能分享你的球剧本吗?还有,是只有一个球,还是你在灌篮或类似的事情后又产生了另一个球?