我无法分配我的游戏对象变量。请协助
我正在尝试将ScriptManager分配给ObjectManager,并使用了该行:
ObjectManager = GameObject.Find("ScriptManager");
我已经检查了多次以确保“ ScriptManager”拼写正确,我什至尝试过将unity直接粘贴到unity中。
运行时我会收到此错误:
“ unapsignedReferenceException:采矿的变量对象管理器 尚未分配。您可能需要分配对象管理员 在检查员中,采矿脚本的变量。 UnityEngine.gameObject.getComponent [t]()(at < 3be1a7ff939c43f181c0a10b5a0189ac>:0)Mining.Sell()(在 资产/采矿。CS:49)“
资产
/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Mining : MonoBehaviour
{
public GameObject ObjectManager;
public Text WorkerCountText;
public float MiningSpeed;
public float WorkersInMine;
public float MiningMultiplyer;
public Collider2D collider;
public GameObject DropDown;
private float WorkerCount;
private float MineWorth;
private float Cash;
// Start is called before the first frame update
void Start()
{
ObjectManager = GameObject.Find("ScriptManager");
collider = GetComponent<Collider2D>();
DropDown.SetActive(false);
}
// Update is called once per frame
void Update()
{
Cash = ObjectManager.GetComponent<GenerateItems>().Money;
WorkerCount = ObjectManager.GetComponent<GenerateItems>().Workers;
MineWorth = ObjectManager.GetComponent<GenerateItems>().MineCost;
WorkerCountText.text = "Workers:" + WorkerCount;
}
public void Sell()
{
ObjectManager.GetComponent<GenerateItems>().Money = Cash + MineWorth;
Object.Destroy (this);
}
}
I am trying to assign ScriptManager to ObjectManager, and used the line :
ObjectManager = GameObject.Find("ScriptManager");
I have checked multiple times to make sure "ScriptManager" is spelt correct, I've even tried copy pasting the name straight from Unity.
I receive this error when running:
"UnassignedReferenceException: The variable ObjectManager of Mining
has not been assigned. You probably need to assign the ObjectManager
variable of the Mining script in the inspector.
UnityEngine.GameObject.GetComponent[T] () (at
<3be1a7ff939c43f181c0a10b5a0189ac>:0) Mining.Sell () (at
Assets/Mining.cs:49)"
I sadly can't assign the variable straight from the inspector, because the object with the code attached is loaded using a Prefab.
here is the full code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Mining : MonoBehaviour
{
public GameObject ObjectManager;
public Text WorkerCountText;
public float MiningSpeed;
public float WorkersInMine;
public float MiningMultiplyer;
public Collider2D collider;
public GameObject DropDown;
private float WorkerCount;
private float MineWorth;
private float Cash;
// Start is called before the first frame update
void Start()
{
ObjectManager = GameObject.Find("ScriptManager");
collider = GetComponent<Collider2D>();
DropDown.SetActive(false);
}
// Update is called once per frame
void Update()
{
Cash = ObjectManager.GetComponent<GenerateItems>().Money;
WorkerCount = ObjectManager.GetComponent<GenerateItems>().Workers;
MineWorth = ObjectManager.GetComponent<GenerateItems>().MineCost;
WorkerCountText.text = "Workers:" + WorkerCount;
}
public void Sell()
{
ObjectManager.GetComponent<GenerateItems>().Money = Cash + MineWorth;
Object.Destroy (this);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用对象
。 .html
Try using Object.FindObjectOfType
https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html
我发现了如何修复它!
对于任何想知道的人来说,被攻击的对象是预制。并且只有在玩家购买该物品后才加载。表示执行start()时未加载它。将线路移入sell(),add(),然后在单击按钮时将其删除。这修复了错误。感谢所有试图提供帮助的人
I have discovered how to fix it!
For anyone wondering, the object this is attacked to is a prefab. And is only loaded once the player purchases the item. Meaning it is not loaded when Start() is executed. Moving the line into Sell() , Add() , and remove assigns it when the buttons are clicked. This fixed the error. Thank you to everyone who tried to help, I am now also using FindGameOBjectWithTag instead of Find to make sure it always works
我将更改您将脚本管理器分配给ObjectManager的方式。函数find()不是很可靠,如果层次结构上有任何变化或以GameObject的名义发生任何变化,则该函数将找不到所需的GameObject。
您可以通过多种方式将GameObject分配给ObjectManager,包括更可靠的函数findObjectOftype(),或简单地创建变量:
public GameObject objectManager;
并将您的对象管理器从层次结构拖到ScriptManager GameObject中的Inspector中。
注意:如果要使用ObjectManager的任何功能,则需要在ScriptManager的启动功能中获取该组件。
I would change the way how you assign the ScriptManager to ObjectManager. The function Find() is not very reliable and in case if anything changes in hierarchy or in the name of the GameObject, the function will not find the desired GameObject.
There are multiple ways how you can assign the GameObject to the ObjectManager, including more reliable function FindObjectOfType(), or simply creating variable:
public GameObject objectManager;
and dragging your ObjectManager from the hierarchy into the inspector in the ScriptManager GameObject.
Note: If you want to use any function of the ObjectManager you would need to get that component in the Start function of the ScriptManager.