2

I'm making a simple game for a school project using Unity. The purpose of the game is to control a ball and pick up coins. The game has 3 scenes. 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.

It works just fine for the first scene (lvl1) but not for the other 2. All 3 scenes have a different number of coins. C# is new to me and I have tried various combinations but it hasn't worked.

How do I re-write this code so that the wintext appears after I pick up the right number of coins on every scene/level?

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!";
        }
       
    }

}
2
  • When you say it does not work on the other scenes do you mean that after picking up 2 coins it fires up the win text? Commented Jan 24, 2019 at 16:04
  • I actually have 88 coins on the first scene, 32 on the second and 31 on the third. I just set at 2 coins so I wouldn't have to complete the whole level just to see if it works. Commented Jan 24, 2019 at 16:13

2 Answers 2

4

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. 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. You will miss a lot of fundamentals. I suggest learning C# without unity for a week or 2 and coming back.

Sign up to request clarification or add additional context in comments.

5 Comments

Just to add something to what storm is saying, if im not mistaken with unity u actually need to set the variables as [Serializable] in order to be able to set them up from the editor
Only if their private. if they're public variables they are already serializable. Hence why I suggested that the OP researches access modifiers.
No if you make a class, you need to mark it as [Serializable] like nalnpir said, unless you refer to scriptableobjects, but that's another story. You are talking about [SerializeField], which is indeed for making private variables show up in the inspector.
@StormMuller, C# isn't a part of my curricular. We are having a 1-month course in Unity at the moment but I'm starting with Java next week. Again, thank you very much =)
@Immorality It's a monobehaviour you do not need to mark it as serializable
0

This code snippet would dynamically set win condition based on scene, however it would be better if the scene could hold coinToCollect variable.

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!";
    }

}

6 Comments

I actually have 88 coins on the first scene, 32 on the second and 31 on the third. I just set at 2 coins so I wouldn't have to complete the whole level just to see if it works.What do you mean by coinToCollect?
coinsToCollect would act as the amount of coins you need to collect for each scene to set the win condition
what exactly do I need to write between the switch parameters and do I need to add something more somewhere else?
void SetCountText() { countText.text = "Coins: " + count.ToString(); int coinsToCollect = 0; switch (robs_scene_lvl1) { case "robs_scene_lvl1": // repeat for other scenes coinsToCollect = 88; break; case "rahils_scene_lvl2": coinsToCollect = 32; break; case "jonas_scene_lvl3": coinsToCollect = 31; break; } if (count >= coinsToCollect) { winText.text = "You Win!"; } }
using a switch statement is just making you life difficult for no reason
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.