Home > Net >  C# - Unable to increment an int beyond 1. Console prints 1 and it does not increase, despite trigger
C# - Unable to increment an int beyond 1. Console prints 1 and it does not increase, despite trigger

Time:01-24

The game is simple, your Player has a running animation and must jump to dodge obstacles, but isn't moving. The obstacles and background are.

My gut instinct is that it is out of scope somehow or is perhaps overflowing - the score increment was increasing far beyond what I had anticipated when I had the AddScore() inside of the Destroy(gameObject); if condition instead of its own function.

However, at this point I am very confused why it isn't working. As a bonus, I cannot get Audio to play from the second commented bit (Value cannot be null.) As for why that happens, no idea. I definitely have the source I have it attached to to the prefab that is spawned, and said spawn should trigger that sound to play when it passes under the player when they jump, I originally thought that there was an issue where the object was deleted before it could reference its audio source but I am unsure.

Edit: I am going to leave the 'bonus' issue above even though I literally fixed it as I typed this up. (I had to add an Audio Source component to the prefab I was spawning.)

I still, for the life of me, cannot get an integer to go above 1 and print to the console. It might be driving me a little insane. Please help friends. I have googled a ton and none of the suggestions from other comments fixed my issue.

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

public class MoveLeft : MonoBehaviour
{
    private float speed = 30;
    private PlayerController playerControllerScript;
    private float leftBound = -15;
    private float scoreZone = -3;
    public AudioClip scoreSound;
    private int score = 0;
    private AudioSource playerAudio;

    // Start is called before the first frame update
    void Start()
    {
        playerControllerScript = GameObject.Find("Player").GetComponent<PlayerController>();
        playerAudio = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        // Moves objects as long as it is not game over.
        if (playerControllerScript.gameOver == false)
        {
            transform.Translate(Vector3.left * Time.deltaTime * speed);
        }
        //Assigns scoring system, buggy. Commented out sound because of errors.
        if (transform.position.x < scoreZone && gameObject.CompareTag("Obstacle"))
        {

            //playerAudio.PlayOneShot(scoreSound, 1);
        }
        //Destroy object out of bounds
        if (transform.position.x < leftBound && gameObject.CompareTag("Obstacle"))
        {
            Destroy(gameObject);
            AddScore();


        }

    }

    void AddScore()
    {
        //Score goes to 1.
        score  ;
        //prints 1 in console but does not increase over 1.
        Debug.Log("Score! Your Score is"   score);
        Debug.Log(score);
    }
}

Tried: Numerous changes to configuration of , x = x 1, x , x =, etc. No idea. Am lost.

CodePudding user response:

This is an issue caused by the local member int. You have an object, which has been created and this MoveLeft component is attached to it. You then destroy this object on collision, and therefore this component as well. You’ll have added one to the local instance int score, but then you destroy the component and lose this local instance.

I suspect what you thought was happening is that all the scripts/components shared the same variable values. That would only be true if you if you made the int score member a static int score, which means the member is the same shared amongst all instances of this component.

Instead of making score static, you might want to have a “Game Manager” that exposes public functions to allow you to increment and retrieve the current score.

CodePudding user response:

add this to initialize you class. Create constructor to this class. after initializing your class it should work fine.

public MoveLeft() { score = 0; }

  • Related