Home>

I'm a noob-newfag. I'm making a hyper-casual game, there are current points and a game record, in the script these are ochki and record, respectively. I want to make that when the record changes, the background color changes randomly once. Well, that is, the record was added by one point, the background color changed once, the record was added by one more point, the background color changed one more time, and so on. Googled, found something and wrote in the Update () method

if (ochki >= record)camera.GetComponent<Camera>().backgroundColor= new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), 1f);

You can say it works, but not as it should, after adding a record by one point, the background color starts to change several times in a second. As I understand it, this is probably due to the Update method. And so the fast random color change continues until the moment when the current points are minus and become again below the record. On the one hand, it seems to be cool, but I would still like to do as I described at the beginning, so that the color changes only once for each new point in the record. And I would like to try the second option, so that the background color changes randomly once every 10 current points are added, here I also don’t understand how to write a formula like if......... that is, how to express such a condition using characters < >= and similar ones. Help please, just in case, what I have now written in Update. And correctly I understand that it is necessary to write in other method?

public void Update()
    {
        if (points &gt;= record) record= points;
        RecordText.text= record.ToString();
        ochkiText.text= ochki.ToString();
        PlayerPrefs.SetFloat("My", record);
        PlayerPrefs.Save();
        if (points &gt;= record)camera.GetComponent&lt;Camera&gt;().backgroundColor= new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), 1f);
    }

So... It's unclear what you don't understand. As far as I understood your task: "to change the background for each point, if there are more points than the record". What is the problem with changing the background every time a point is added, provided there are more points than the high score? Do you have a feature to add points? Update is generally (most likely) not needed here, since the player does not receive points every frame.

Максим Фисман2022-02-14 13:40:37
  • Answer # 1

    You're right, you can't do this in Update, because there will be problems like yours. It is better to use events in this case. Take out the code that is responsible for the account in a separate class.

    Something like this:

    using System;
    public class Score
    {
        public event Action ScoreChanged; //Define an event
        private int_score= 0;
        public void IncreaseScore(int amount) //Increase the score by a certain amount
        {
            _score += amount;
            ScoreChanged?.Invoke(); //call an event when the score changes
        }
    }
    

    Here you define an event and when the count changes, all subscribers of this event will call the defined method.

    Next, I show you how to subscribe to an event:

    using UnityEngine;
    public class SomeClass : MonoBehavior
    {
        private Score_score; //check
        private void Start()
        {
            _score.ScoreChanged += DoStuff; //Subscribe to the event, when ScoreChanged?.Invoke() happens, the DoStuff() method will be called
        }
        private void OnDestroy()
        {
            _score.ScoreChanged -= DoStuff; //Unsubscribe from the event so that no errors come out if the SomeClass object is removed from the scene
        }
        private void DoStuff()
        {
            debug.Log("1234");
        }
    }
    

    Now when your account changes, the method is calledDo Stuff, which writes to the console -1234. This is where you sign the methodDoStuff() per eventScoreChangedwith the help of an operator+=. It is important that the signatures of the method and event delegate match, in this case this means thatDo Stuffhas no arguments and event typeScoreChangedis an Action, not an Action.

    If you need to perform any operations with the account, you can change the event type fromactionon theAction<int> and, accordingly, the methodDoStuff()on theDoStuff(int count)

    Also, you can subscribe inOnEnable(), and unsubscribe, respectively, inOnDisable(). The main thing is to never forget to unsubscribe from the event, otherwise it can be fraught with errors that will be difficult to find.

    It's up to you how you want to change the background color. a huge number of ways. You need to change the background in the methodDoStuff().

    You can read more about events on sites like metanit and others.

  • Answer # 2

    You're right, you can't do this in Update, because there will be problems like yours. It is better to use events in this case. Take out the code that is responsible for the account in a separate class.

    Something like this:

    using System;
    public class Score
    {
        public event Action ScoreChanged; //Define an event
        private int_score= 0;
        public void IncreaseScore(int amount) //Increase the score by a certain amount
        {
            _score += amount;
            ScoreChanged?.Invoke(); //call an event when the score changes
        }
    }
    

    Here you define an event and when the count changes, all subscribers of this event will call the defined method.

    Next, I show you how to subscribe to an event:

    using UnityEngine;
    public class SomeClass : MonoBehavior
    {
        private Score_score; //check
        private void Start()
        {
            _score.ScoreChanged += DoStuff; //Subscribe to the event, when ScoreChanged?.Invoke() happens, the DoStuff() method will be called
        }
        private void OnDestroy()
        {
            _score.ScoreChanged -= DoStuff; //Unsubscribe from the event so that no errors come out if the SomeClass object is removed from the scene
        }
        private void DoStuff()
        {
            debug.Log("1234");
        }
    }
    

    Now when your account changes, the method is calledDo Stuff, which writes to the console -1234. This is where you sign the methodDoStuff() per eventScoreChangedwith the help of an operator+=. It is important that the signatures of the method and event delegate match, in this case this means thatDo Stuffhas no arguments and event typeScoreChangedis an Action, not an Action.

    If you need to perform any operations with the account, you can change the event type fromactionon theAction<int> and, accordingly, the methodDoStuff()on theDoStuff(int count)

    Also, you can subscribe inOnEnable(), and unsubscribe, respectively, inOnDisable(). The main thing is to never forget to unsubscribe from the event, otherwise it can be fraught with errors that will be difficult to find.

    It's up to you how you want to change the background color. a huge number of ways. You need to change the background in the methodDoStuff().

    You can read more about events on sites like metanit and others.