简体   繁体   English

如何链接我的C#脚本,以便项目数影响Unity游戏中的得分?

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

相关问题 如何将我的分数游戏 Object 从我的游戏场景转移到 Unity 中的游戏结束场景 - How can I Transfer my Score Game Object from my game scene to game over scene in Unity 如何在 Unity 2d 中为我的乒乓球比赛得分 - How can I make score in Unity 2d for my pong game (Visual Studio) 如何将 powershell 脚本导入 c# 项目,以便我可以将 .NET 应用程序与脚本一起发布 - (Visual Studio) How to import powershell scripts into c# project so that I can publish my .NET application together with the scripts Unity C#-如何做到这一点,以便可以通过一个类访问所有其他类,变量和方法? - Unity c# - How can I make it so that I can access all my other classes, variables and methods via a single class? C#:如何在Unity中使用数组而不是列表重写用于管理游戏数据序列化的代码? - C#: How can I rewrite my code for managing Game Data Serialization using an Array instead of a List in Unity? 如何为C#XNA游戏制作GUI? - How can I make a GUI for my C# XNA game? 如何在 Unity C# 中更改我的 TicTacToe 游戏中的移动? - How to change move in my TicTacToe game in Unity C#? 如何在另一个脚本if语句中使用移动输入结果? C#Unity - How do I use my mobile input results in another scripts if statement? C# Unity unity c# 游戏得分问题 - Issue with score in game with unity c# 如何使克隆的游戏对象成为全局对象,以便可以在Unity功能之外使用它们? - How do I make my cloned game objects global, so I can use them outside of the function in Unity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM