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

I have simple doubts(I don't have much experience)

Discussion in 'Scripting' started by lilslyjay, Jul 7, 2020.

  1. lilslyjay

    lilslyjay

    Joined:
    Jul 6, 2020
    Posts:
    2
    hello ...so I was making some crates u can grab and throw(for a game)...I did it in weird way(I guess)
    but my question is: all my crates in the scene behave the same(they r separated objects with the same script)
    when u grab one and throw it and grab the other it changes their falling speed...I had the same problem with enemies health...and somehow I solved it but I don't remember how....
    thanks in advance
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    I don't understand. Are you asking a question? What are you needing help with?
     
    bobisgod234 likes this.
  3. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Total blind stab in the dark, but your crates falling speed wouldn't happen to be a static variable would it?
     
    PraetorBlue likes this.
  4. lilslyjay

    lilslyjay

    Joined:
    Jul 6, 2020
    Posts:
    2
    yeah...nice stab ^^...it was not the speed but a static bool
    (to confirm if the object was being held by the player)
    (now I think I've learned what static variable...is)
    so no static stuff for game objects that I'll use repeatedly right?
     
    PraetorBlue and bobisgod234 like this.
  5. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    I would recommend reading up on what a static variable is. They are very easy to abuse and very error prone if you don't fully understand what they are. Especially avoid using them to make accessing variables of your scripts easier.

    Normal variables are unique to each instance of an object. Static variables are exactly the same for each variable of an object. If you are going to have multiple of something, then static variables generally don't make sense.

    You rarely would use a static variable at all.