简体   繁体   English

gameobject object 变量未分配,我如何在脚本中统一分配?

[英]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.

相关问题 如何在统一 c# 中从实例化的 object 中的脚本引用游戏对象? - How do I reference a GameObject from a script inside an instantiated object in unity c#? 如何在统一C#脚本中为游戏对象设置材质? - How do I set a material to an gameobject in unity c# script? 尝试将 GameObject 分配给另一个脚本中的变量(Unity C#) - Trying to assign GameObject to variable in another script (Unity C#) 如何将C#对象与Unity游戏对象相关联? - How do I associate a C# object with a Unity gameobject? 如何在 Unity 中另一个游戏对象的转换 position 实例化 object? - How do I Instantiate an object at the transform position of another GameObject in Unity? 如何在选定的GameObject下方获取GameObject的脚本? (Unity2D)(C#) - How do I get the script of a GameObject underneath my selected GameObject? (Unity2D) (C#) 如何为 Unity 中由 OnTriggerEnter 触发的游戏对象分配新的 transform.position? - How do I assign a new transform.position for a GameObject that's triggered by an OnTriggerEnter in Unity? 如何在 Unity 中获取分配给游戏对象的所有材质 - How to get all the materials assigned to a GameObject in Unity 如何将 int 变量引用到 Unity 中另一个 GameObject 中的另一个脚本? - How to reference the int variable to another script which is in another GameObject in Unity? 如何在 Unity 中获取对随机生成的子 Gameobject 脚本的引用? - How do I get a reference to a randomly generated child Gameobject's script in Unity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM