Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Resolved Need help with Debug.Log Prototype 2.

Discussion in 'Scripting' started by JeaNiX, Jan 25, 2021.

  1. JeaNiX

    JeaNiX

    Joined:
    Oct 31, 2016
    Posts:
    20
    Hello there,
    I was working on prototype 2 from the programming course, and there was a task to add a game over string to the console. It was not hard, but than I decided to return the number of the animals that go to the bottom line. Was searching for a solution for hours, used for and do while, nothing is working, its just returning 1.
    I am indeed a beginner here, maybe someone can give me a tip, just can't continue without solving this.
    Thanks!
    public class DestroyOutOfBounds : MonoBehaviour
    {
    private float topBound = 30.0f;
    private float lowerBound = -10.0f;
    private int save = 0;

    void Start()
    {

    }
    void Update()
    {
    if (transform.position.z > topBound)
    {
    Destroy(gameObject);
    }
    else if (transform.position.z < lowerBound)
    {
    Destroy(gameObject);
    save = save + 1;
    Debug.Log(save);
    }
    }
    }

    This is an edited light script, I was previously using more like 4 variables and another if statement and it did the same thing.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    Your problem is that each instance of this script has its very own copy of all the variables inside of it. That's why it always prints 1, because it starts at 0 and you add 1, and then that variable goes away with your object when it's destroyed.

    You can make all the instances of the script share the same single copy of the variable if you make it static:
    Code (CSharp):
    1. private static int save = 0;
    Static means "this variable is not part of a particular instance of this script, it simply is associated with the script itself".
     
    Bunny83, JeaNiX and Joe-Censored like this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    @PraetorBlue sneaks in while I'm typing the same thing yet again :p
     
    Bunny83, JeaNiX and PraetorBlue like this.
  4. JeaNiX

    JeaNiX

    Joined:
    Oct 31, 2016
    Posts:
    20
    Thank you so much. I was searching in the wrong direction. Now this will be one of many lessons I'll get. Wasn't even thinking that the script is copying itself with every object. Will remember that. Thanks.)
     
  5. Boston2385

    Boston2385

    Joined:
    Jan 13, 2023
    Posts:
    1
    i in unity when i get to the part to spawn the animal tons of animal's spawn and my length won't work can't figure out what i did wrong, please help still new and learning.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Then be sure to do yourself a favor and NOT necro-post to two-year-old threads.

    Make a new post... it's FREE!

    When you post, remember we cannot read your mind and we cannot see your computer.

    YOU must communicate effectively and clearly. Here's how:

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    Bunny83 likes this.