How Can I Fix this error code for my Star Rating System

I am trying to create a 3 star rating system based off the score the player gets. Here is my code

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

public class starz : MonoBehaviour


{

    public GameObject[] stars;
    public GameObject scoreText;

    // Start is called before the first frame update
    void Start()
    {
        if (scoreText <= 30)
        {
            stars[0].SetActive(true);
            stars[1].SetActive(false);
            stars[2].SetActive(false);
        }   
        else if (scoreText <= 60)
        {
            stars[0].SetActive(false);
            stars[1].SetActive(true);
            stars[2].SetActive(false);
        }
        else
        {
            stars[0].SetActive(false);
            stars[1].SetActive(false);
            stars[2].SetActive(true);
        }
    }

I get the error code Operator '<=' cannot be applied to operands of type 'GameObject' and 'int'. When I replace public GameObject with a public int the error code goes away. But I cannot use int because I cannot place the variable which would be the player's score in the editor. I am a beginner; can any explain to me what I am doing wrong or if my problem is too complex for a beginner?

Not sure how much clearer this can be.

scoreText is a GameObject, you’re comparing it to an int.

Hi there,

You comparing two variable types which cannot be compared.

GameObject and intiger value.

For your scores best vill be
[SerializedField] int m_amountOfScores;

Then if you need some text from it change it for string, like this:

m_amountOfScores.ToString();

You write:
I cannot place the variable which would be the …

Can imagine that… you creating new GameObjects, changing name for amount of score, and assign in inspector… but hope is only my bad imagination… if yes, stop it… then declare this int variable as mentioned above… assign values (by keyboard) in inspector tab… and watch the magic when entering play mode…

Yes, I understand that they are two different thing, but I am understanding it better. I will try to compare two objects instead, if that’s a way to fix it

Thank you, I will try this out :slight_smile:

1 Like

Please don’t use the 2D tag unless your post is asking about 2D features. I’ve removed that and added a “Scripting” tag for you.

Thanks.

Sorry I thought it was for if I was using unity 2d in general. Thanks

In addition to fixing the bug you might take this opportunity to refactor a bit. The idea is to reduce complexity without obscuring purpose.

using UnityEngine;


public class starz : MonoBehaviour
{
    public GameObject[] stars;
    public int score;

    private void Start()
    {
        var star = GetStarCount(score);

        stars[0].SetActive(star == 1);
        stars[1].SetActive(star == 2);
        stars[2].SetActive(star == 3);
    }


    private int GetStarCount(int score)
    {
        if (score <= 30)
            return 1;

        if (score <= 60)
            return 2;

        return 3;
    }
}

Wow! I tried this method and it worked! Thanks!

You are welcome… and “of course it worked” :smile:

There’s no such thing as Unity 2D, only 2D features. The 2D tag is for when you’re asking specifically about 2D features.

Thanks.