[英]gameobject object variable is not assigned, how do i assign this in script in unity?
I have tried to assign my prefab Bridge
in many different ways.我试图以许多不同的方式分配我的预制
Bridge
。
I still get an error saying "variable of bridge in GameManager has not been assigned"我仍然收到错误消息“尚未分配 GameManager 中的桥接变量”
Is there anyway to fix this issue please, Would be more than happy an appreciative if anyone can help me out!请问有没有办法解决这个问题,如果有人可以帮助我,将不胜感激!
public GameObject Bridge;
public GameObject player;
public float spawnSpacer = 10f;
public float rightSpacer = 10f;
public float leftSpacer = 10f;
int spawnNum;
public float spawnDelay = 1f;
public List<Movement> bridges;
bool left;
private void Start()
{
InvokeRepeating(nameof(InitialSpawns), spawnDelay, spawnDelay);
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
StopBridge();
SpawnBridge();
}
}
public void StopBridge()
{
if (bridges.Count != 0)
{
bridges[0].locked = true;
bridges.Remove(bridges[0]);
}
}
public void SpawnBridge()
{
//Determine spawn location by manipulating player position.
Vector3 spawnPosition = player.transform.position;
spawnPosition.x += spawnSpacer;
if (left)
{
spawnPosition.z += leftSpacer;
left = false;
}
else if (!left)
{
spawnPosition.z -= leftSpacer;
left = true;
}
//Spawn a new platform.
GameObject newBridge = Instantiate(Bridge, spawnPosition, player.transform.rotation);
if (left)
newBridge.GetComponent<Movement>().left = true;
else if (!left)
newBridge.GetComponent<Movement>().right = true;
bridges.Add(newBridge.GetComponent<Movement>());
}
void InitialSpawns()
{
if (spawnNum == 5)
CancelInvoke(nameof(InitialSpawns));
SpawnBridge();
spawnNum++;
}
} }
What is happening if you try to drag the object "Bridge" from the hierarchy to inspector in editor?如果您尝试将 object “Bridge”从层次结构拖动到编辑器中的检查器,会发生什么情况? if it is still not working then to set it via script use:
如果它仍然不起作用,则通过脚本使用进行设置:
public GameObject Bridge = GameObject.Find("ObjectName");
alternatively, you can also use the tag of the object to find it:或者,您也可以使用 object 的标签来找到它:
public GameObject Bridge = GameObject.FindGameObjectWithTag("GameObject Tag"). gameObject;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.