[英]How can I link my C# scripts so that item count influences score in my Unity game?
I am trying to make it so that collecting an item will cause the score to increase. 我正在努力做到这一点,以便收集物品会导致分数增加。 After perusing the documentation for Unity, I have written the following C# scripts.
在仔细阅读Unity文档之后,我编写了以下C#脚本。 CollectableItem.cs is attached to the items in the game.
CollectableItem.cs附加到游戏中的项目。 ScoreBoard.cs is attached to the UI display.
ScoreBoard.cs附加到UI显示。 I'm getting the error message, "'ScoreBoard.score' is inaccessible due to its protection level."
我收到错误消息,“'ScoreBoard.score'由于其保护级别而无法访问。” If I make the variables in ScoreBoard.cs public, I get a different error message: "An object reference is required for the non-static field, method, or property 'ScoreBoard.score'."
如果将ScoreBoard.cs中的变量设为公共,则会收到另一条错误消息:“非静态字段,方法或属性'ScoreBoard.score'需要对象引用。”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollectibleItem : MonoBehaviour {
[SerializeField] private string itemName;
[SerializeField] private int pointsValue;
void OnTriggerEnter(Collider other) {
Managers.Inventory.AddItem(itemName);
Destroy(this.gameObject);
ScoreBoard.score += pointsValue;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreBoard : MonoBehaviour {
[SerializeField] private Text scoreLabel;
private int score;
void Start () {
score = 0;
}
void Update () {
scoreLabel.text = "Score: " + score.ToString();
}
}
UPDATE: Here is Take 2 on CollectibleItem.cs. 更新:这是CollectibleItem.cs上的Take 2。 Now I'm being informed that "board" does not exist in the current context...
现在,我被告知在当前上下文中不存在“木板” ...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollectibleItem : MonoBehaviour {
[SerializeField] private string itemName;
[SerializeField] private int pointsValue;
void Start() {
var uiObject = GameObject.Find("Timer");
ScoreBoard board = uiObject.GetComponent<ScoreBoard>();
}
void OnTriggerEnter(Collider other) {
Managers.Inventory.AddItem(itemName);
Destroy(this.gameObject);
board.score += pointsValue;
}
}
This is not able to work because you make a so-called static access to the ScoreBoard class. 这不能工作,因为您对ScoreBoard类进行了所谓的静态访问 。 That means you try to change the variable of the ScoreBoard class.
这意味着您尝试更改ScoreBoard类的变量。 What you want to do is to change the variable on one of the instances.
您要做的是更改实例之一上的变量。 When the UI Object is created, an instance of the class of your ScoreBoard-Script is created.
创建UI对象时,将创建ScoreBoard-Script类的实例。 Like every item has its own instance of CollectibleItem.
就像每个项目都有自己的CollectibleItem实例一样。 You can get the instance in this way:
您可以通过以下方式获取实例:
var uiObject = GameObject.Find("Name of UI Object");
ScoreBoard board = uiObject.GetComponent<ScoreBoard>();
You can do this in Start() and save the ScoreBoard variable in the script where your other private variables reside and use it later in the trigger, or you can do this directly in your Trigger function and directly set the score: 您可以在Start()中执行此操作,并将ScoreBoard变量保存在其他私有变量所在的脚本中,并稍后在触发器中使用它,也可以直接在Trigger函数中执行此操作并直接设置分数:
board.score += pointsValue;
EDIT: You have to place the declaration of Scoreboard inside the class: 编辑:您必须将记分板的声明放在类内:
ScoreBoard board;
void Start ()
...
Or put the code from start to OnTriggerEnter. 或将代码从开始放置到OnTriggerEnter。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.