我正在制作一个基本的篮球比赛,我有一个名为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);
}
}
}
任何帮助都会有很大的帮助。
答案 0 :(得分:0)
你的basketBallScript没有引用你实例化的球对象的ball_script。您应该保留对您创建的GameObject的引用,然后分配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);
}
}
}
另外,为了让您的代码在将来更容易关注其他代码,您可能需要查看general naming conventions。