简体   繁体   English

Unity中特定项目的C#编码问题

[英]Problems with C# coding in Unity for a specific project

I'm making a simple game for a school project using Unity. 我正在使用Unity为学校项目制作一个简单的游戏。 The purpose of the game is to control a ball and pick up coins. 游戏的目的是控制球并捡起硬币。 The game has 3 scenes. 游戏中有3个场景。 I have written some code in C# to count my pick up coins and set a condition to check if all coins are picked up, if so, a wintext appears at the center of the screen. 我已经用C#编写了一些代码来计数我捡起的硬币,并设置了一个条件来检查是否捡起了所有硬币,如果这样,一个wintext出现在屏幕中央。 It works just fine for the first scene (lvl1) but not for the other 2. All 3 scenes have a different ammount of coins. 它对第一个场景(lvl1)正常,但对其他2个场景无效。所有3个场景的硬币数量都不相同。 C# is new to me and I have tried various combinations but it hasn't worked. C#对我来说是新手,我尝试了各种组合,但没有用。 How do I re-write this code so that the wintext appears after I pick up the right ammount of coins on every scene/level? 我如何重新编写此代码,以便在我在每个场景/级别拾取正确数量的硬币后出现wintext? This is my code: 这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{

    public float speed;
    public Text countText;
    public Text winText;

    private Rigidbody rb;
    private int count;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        count = 0;
        SetCountText();
        winText.text = "";

    }

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

        rb.AddForce(movement * speed);
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Pick Up"))
        {
            other.gameObject.SetActive(false);
            count = count + 1;
            SetCountText();

        }
    }

    void SetCountText()
    {
        countText.text = "Coins: " + count.ToString();
        if (count >= 2)
        {
            winText.text = "You Win!";
        }

    }

}

Make a new public variable 设置一个新的公共变量

...
public float speed;
public Text countText;
public Text winText;
public int numberOfCoinsToWin;
... 

remember to set this new value in the editor for each scene 记得在编辑器中为每个场景设置此新值

Use the variable in your condition. 根据您的条件使用变量。

if (count >= numberOfCoinstoWin)
{
    winText.text = "You Win!";
}

Sounds like you're lacking a very basic understanding of C# and programming in general. 听起来您通常对C#和编程缺乏基本的了解。 Here are somethings you could research to make life easier for you: 您可以研究以下内容,以使您的生活更轻松:

  • variables 变量
  • control flow 控制流
  • access modifiers 访问修饰符
  • classes (in computer science) 类(计算机科学)
  • object orientation 面向对象

Also using Unity to learn C# is not great. 同样使用Unity学习C#也不是很好。 You will miss a lot of fundamentals. 您将错过很多基础知识。 I suggest learning C# without unity for a week or 2 and coming back. 我建议学习C#在没有团结的情况下持续一到两个星期再回来。

This code snippet would dynamically set win condition based on scene, however it would be better if the scene could hold coinToCollect variable. 该代码段将根据场景动态设置获胜条件,但是如果场景可以coinToCollect变量,则会更好。

void SetCountText()
{
    countText.text = "Coins: " + count.ToString();
    int coinsToCollect = 0;
    switch( /* get current scene here */)
    {
        case "scene1": // repeat for other scenes
            coinsToCollect = 2;
            break;
    }
    if (count >= coinsToCollect)
    {
        winText.text = "You Win!";
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM