Search Unity

I don't get why "public float" does not work

Discussion in 'Scripting' started by clarioncat, Oct 8, 2013.

Thread Status:
Not open for further replies.
  1. clarioncat

    clarioncat

    Joined:
    Oct 7, 2013
    Posts:
    2
    Hey guys,

    I just began teaching myself C# through Tutorials. Has been working out great so far.
    It's just that sometimes I stumble upon things that don't work out for me, and then I find a workaround solution but I really HATE it when I don't understand what the problem was…

    I created a prefab for an enemy object which is supposed to move at random speed, for which I used "currentSpeed" with "Random.Range(minSpeed, maxSpeed)" with those two being "public" float variables. Thing is, when playing Unity didn't read "min/maxSpeed" but instead used other values unknown to me.

    It didn't work out until I changed the variables "min/maxSpeed" from public to private. Then it worked totally fine.

    Could anybody explain to me why that is?
    Thanks in advance!

    I

    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4.  
    5. public class Enemy : MonoBehaviour {
    6.  
    7.     Transform myTransform;
    8.     public float minSpeed = 2.0f;
    9.     public float maxSpeed = 8.0f;
    10.     int x, y, z;   
    11.     public float currentSpeed;
    12.    
    13.     void Start () {
    14.    
    15.         x = Random.Range(-8, 8);
    16.         y = 6;
    17.         z = -1;
    18.        
    19.         myTransform = transform;
    20.         myTransform.position = new Vector3(x, y, z);
    21.         currentSpeed = Random.Range(minSpeed, maxSpeed);
    22.    
    23.    
    24.     }
    25.    
    26.     void Update ()
    27.     {          
    28.         x = Random.Range(-8, 8);
    29.        
    30.         myTransform.Translate(Vector3.down * currentSpeed * Time.deltaTime);
    31.        
    32.             if (myTransform.position.y < -5)
    33.             {
    34.                 myTransform.position = new Vector3(x, y, z);
    35.                 currentSpeed = Random.Range(minSpeed, maxSpeed);
    36.             }
    37.        
    38.     }
    39.        
    40.  
    41.  
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Most likely, when you first created this script and attached it to your prefab you gave a different value for minSpeed and maxSpeed (or no value which defaults to 0). When a public variable is changed in script, it is not changed on any GameObject it is attached to.

    Changing the variables to private fixed the issue because then the value was forced onto every GameObject the script is attached to.
     
    boyoboy likes this.
  3. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    This looks very familiar, I could be wrong, but is this my series?

    As DanielQuick said, when working with prefabs, don't forget to hit: Apply all on the top right of the inspector to change whatever values you are changing. The script looks good.

    Please let us know if you still are receiving problems.

    Best,

    Jon
     
  4. clarioncat

    clarioncat

    Joined:
    Oct 7, 2013
    Posts:
    2
    Ha, that solves it and also helped me understand what I needed to understand.
    This was exactly the background information I was looking for; for some reason I thought the script would need to override the prefab values in the inspector. Now I see why it makes sense that this is not the case.

    Thanks to both of you for helping me out here!!

    @Jon:

    You got that right! I am having so much fun with your tutorial right now… I wanted to write to you anyway after finishing the whole series, just for saying HOW MUCH I love what you did there. Really, I couldn't do it any justice here, but just know that this was the first thing I couldn't figure out for myself, I've learned heaps and heaps!!!
     
  5. usaidmohd21

    usaidmohd21

    Joined:
    Jan 23, 2020
    Posts:
    1
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. public class Enemy : MonoBehaviour {
    4.     Transform myTransform;
    5.     private float minSpeed = 2.0f;
    6.     private float maxSpeed = 8.0f;
    7.     int x, y, z;  
    8.     public float currentSpeed;
    9.  
    10.     void Start () {
    11.  
    12.         x = Random.Range(-8, 8);
    13.         y = 6;
    14.         z = -1;
    15.      
    16.         myTransform = transform;
    17.         myTransform.position = new Vector3(x, y, z);
    18.         currentSpeed = Random.Range(minSpeed, maxSpeed);
    19.  
    20.  
    21.     }
    22.  
    23.     void Update ()
    24.     {        
    25.         x = Random.Range(-8, 8);
    26.      
    27.         myTransform.Translate(Vector3.down * currentSpeed * Time.deltaTime);
    28.      
    29.             if (myTransform.position.y < -5)
    30.             {
    31.                 myTransform.position = new Vector3(x, y, z);
    32.                 currentSpeed = Random.Range(minSpeed, maxSpeed);
    33.             }
    34.      
    35.     }
    36.        
    using System.Collections;

    public class Enemy : MonoBehaviour {

    Transform myTransform;
    private float minSpeed = 2.0f;
    private float maxSpeed = 8.0f;
    int x, y, z;
    public float currentSpeed;

    void Start () {

    x = Random.Range(-8, 8);
    y = 6;
    z = -1;

    myTransform = transform;
    myTransform.position = new Vector3(x, y, z);
    currentSpeed = Random.Range(minSpeed, maxSpeed);


    }

    void Update ()
    {
    x = Random.Range(-8, 8);

    myTransform.Translate(Vector3.down * currentSpeed * Time.deltaTime);

    if (myTransform.position.y < -5)
    {
    myTransform.position = new Vector3(x, y, z);
    currentSpeed = Random.Range(minSpeed, maxSpeed);
    }

    }
     
  6. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,997
    Note that it's not a prefab issue -- it's a funny thing Unity does with public variables with any script on any gameObject, even ones starting in the scene. Programming isn't broken: public int x=5; really sets x to 5. But then Unity's special rules, before anything else happens, copies in the Inspector values. The whole situation is like this:

    Code (CSharp):
    1. public int x=5; // this really happens, and sets x to 5
    2. // but the next step is for Unity to jump in and apply Inspector values, erasing the 5
    3.  
    4. void Start() {
    5.   // Start is the 4th step, after creation, Inspector and Awake()
    6.   x=10; // this erases the Inspector value
    7. }
    The net effect, which is kind of weird, is the x=5 is only for the starting Inspector value when the script goes on a new gameObject. But altogether it makes sense -- people who don't know programming get to tweak public vars "for real" by using the Inspector.
     
  7. bestrustplayer

    bestrustplayer

    Joined:
    Jun 8, 2022
    Posts:
    1
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.  
    8.     // variables
    9.     public float maxHealth, maxThirst, maxHunger;
    10.     public float thirstIncreaseRate, hungerIncreaseRate;
    11.     private float health, first, hunger;
    12.  
    13.  
    14.  
    15.     // functions
    16.     public void Start()
    17.     {
    18.         health = maxHealth;
    19.     }
    20.  
    21.     public void Update()
    22.     {
    23.  
    24.         // thirst and hunger increase
    25.         if(thirst < maxThirst)
    26.         {
    27.             thirst += thirstIncreaseRate * Time.deltaTime;
    28.         }
    29.  
    30.         if(hunger < maxHunger)
    31.         {
    32.             hunger += hungerIncreaseRate * Time.deltaTime;
    33.         }
    34.  
    35.         if (thirst >= maxThirst)
    36.         Die();
    37.         if (hunger >= maxHunger)
    38.         Die();
    39.  
    40.      
    41.  
    42.     }
    43.  
    44.  
    45.  
    46.  
    47.     public void Die()
    48.     {
    49.         print("You have died because of hunger or thirst");
    50.     }
    51.  
    52. }
    53.  
    i dont know why but whenever i do a public float it never shows up in unity
     

    Attached Files:

  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,859
    Don't need to necro old posts when you can make your own new post.

    Nonetheless does your script have any compilation errors? Are there other compilation errors in other scripts?
     
    Bunny83 likes this.
  9. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    I'm going to go out on a limb here and say that it does have errors. In their
    Update
    method they make reference to a
    thirst
    variable, but there is no such variable as it looks like they defined it as
    first
    .
     
    Kurt-Dekker and Bunny83 like this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    It's because you have compiler errors. Go fix them.

    Make sure your log console selector buttons are enabled. See this graphic:

    https://forum.unity.com/threads/cant-add-script-component.632746/#post-4239121

    https://forum.unity.com/threads/update-function-not-working.953477/#post-6215873

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
Thread Status:
Not open for further replies.