Home > OS >  Why does my int change when my array changes? (Unity)
Why does my int change when my array changes? (Unity)

Time:07-07

So i have been making this evolution game in Unity with chickens, and i am pretty new to developing games. So my intention was to make an array that counts all the chickens in the game, and then i would make an int, that shows how many eggs the chickens had made. But when i spawn a new chicken, the number of eggs in total, sets to 0. The code i use comes here.

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

public class EggLayer : MonoBehaviour
{
    public GameObject Egg;

    public GameObject Chicken;

    public Text EggText;

    public int EggNumber = 0;

    public float NewEggTime = 2f;

    // Start is called before the first frame update
    void Start()
    {
        Egg.SetActive(false);
        StartCoroutine("MakeEggVisible");
    }

    // Update is called once per frame
    void Update()
    {
        EggText.text = EggNumber.ToString("0");
    }

    IEnumerator MakeEggVisible()
    {

        GameObject[] ChickenToFind = GameObject.FindGameObjectsWithTag("Chicken");

        int ChickenCount = ChickenToFind.Length;

        while (true)
        {
            yield return new WaitForSeconds(NewEggTime);
            EggNumber = EggNumber   1 * ChickenCount;
            Egg.SetActive(true);
            yield return new WaitForSeconds(NewEggTime);
            Egg.SetActive(false);
        }
    }

}

How do i fix this. Please help me out of this small error.

CodePudding user response:

int ChickenCount = ChickenToFind.Length; //This is never updated. Outside of the while loop

A possible fix is placing the chickenCount integer at the beginning of the while loop like so and changing the formula to

EggNumber = (EggNumber   1) * ChickenCount;

This will be the code example.

    while (true)
    {
        int ChickenCount = ChickenToFind.Length;
        yield return new WaitForSeconds(NewEggTime);
        EggNumber = (EggNumber   1) * ChickenCount;
        Egg.SetActive(true);
        yield return new WaitForSeconds(NewEggTime);
        Egg.SetActive(false);
    }

CodePudding user response:

I think this is a very specific question. Long term you are probably better of watching e.g. this video on "how to debug in Unity": https://www.youtube.com/watch?v=d0815qbx3BA

But anyway:

I'm not sure if I really got what you are trying to do with:

IEnumerator MakeEggVisible()
{
    GameObject[] ChickenToFind = GameObject.FindGameObjectsWithTag("Chicken");

    int ChickenCount = ChickenToFind.Length;

    while (true)
    {
        yield return new WaitForSeconds(NewEggTime);
        EggNumber = EggNumber   1 * ChickenCount;
        Egg.SetActive(true);
        yield return new WaitForSeconds(NewEggTime);
        Egg.SetActive(false);
    }
}

What this function does is basically "every few seconds count up EggNumber by the initial number of chickens"

I guess you tried to add the total number of chickens every few seconds. (Maybe because every chicken lays an egg?) For that you would need to get all chickens again every time the function is called:

IEnumerator MakeEggVisible()
{
    while (true)
    {
        GameObject[] ChickenToFind = GameObject.FindGameObjectsWithTag("Chicken");
        int ChickenCount = ChickenToFind.Length;
        yield return new WaitForSeconds(NewEggTime);
        EggNumber  = ChickenCount;
        Egg.SetActive(true);
        yield return new WaitForSeconds(NewEggTime);
        Egg.SetActive(false);
    }
}

This however does not explain your problem that the number resets when a new chicken is spawned. It could have something to do with

public GameObject Chicken;

as part of the EggLayer. But it could also be possible the error is in the location where the chicken is spawned.

  • Related