Search Unity

keeping variable same across two objects

Discussion in 'Getting Started' started by skeleton-king, Aug 27, 2015.

Thread Status:
Not open for further replies.
  1. skeleton-king

    skeleton-king

    Joined:
    Nov 10, 2014
    Posts:
    63
    I have two buttons and a button manager script attached to left and right button.
    I have a variable num =1;
    i wana increase num by 1 when i press the right button and decrese when i press left.
    However when i press right button it does increases by 1 but when i press left button the count starts from 1 again and not where i left it when i had pressed right button.

    Any1 knows how to fix this
     
  2. Martin_H

    Martin_H

    Joined:
    Jul 11, 2015
    Posts:
    4,436
    That sounds like a problem that you should be able to figure out yourself by looking at your code line by line and making yourself aware of what each line does.

    The reason it does what it does, is in all likelyhood that you programmed it that way.

    You should give yourself another chance at fixing this yourself, it will be a rewarding experience if you do it on your own. If you really can't figure it out, post the code here (in code tags!) and explain where exactly you don't understand what it does.
     
  3. skeleton-king

    skeleton-king

    Joined:
    Nov 10, 2014
    Posts:
    63
    private int nm = 1;
    public void slide()
    {
    if (nm !=4 && gameObject.name=="slider_r") {
    nm+=1;


    print (nm);

    }
    if ( gameObject.name=="slider_l") {
    nm-=1;

    print (nm);

    }

    }

    Heres code. Its on both buttons slider_r and slider_l
    When i press slider_r button twice nm is 3 as expected, but now if i press slider_l button once nm should be 2 but it comes as 0.
     
  4. Martin_H

    Martin_H

    Joined:
    Jul 11, 2015
    Posts:
    4,436
    When I said you should code tags, I meant posting your code like this:
    Code (csharp):
    1. [code] your code goes here [/ code] <-- // but remove the space before 'code' in the closing tag!!! I just had to put that there so it visible at all.
    It makes code snippits waaaaay more readable and increases the likelyhood of you getting an answer to your question by a lot.


    Code (csharp):
    1. private int nm = 1;
    2. public void slide()
    3. {
    4.   if (nm !=4 && gameObject.name=="slider_r") {
    5.     nm+=1;
    6.     print (nm);
    7.   }
    8.   if ( gameObject.name=="slider_l") {
    9.     nm-=1;
    10.  
    11.     print (nm);
    12.  
    13.   }
    14.  
    15. }
    16.  

    The first line is already wrong for what you want to achieve. You have a script and declare a private int variable for the script and initialize it to 1. Then you put instances of that script on your two sliders/buttons. Each one has their own private variable now. Those are not connected in any way. Each button deals with his number by the same logic, since they have the same script. So they both start out on 1. If you press the right button twice you start at 1 and make it 3. If you press the other button you start at 1 and it becomes 0 when you click once, just like you programmed it to.

    You need to read this:
    http://answers.unity3d.com/questions/42843/referencing-non-static-variables-from-another-scri.html

    And learn some programming basics. This book has been recommended:
    http://www.robmiles.com/c-yellow-book/
     
  5. skeleton-king

    skeleton-king

    Joined:
    Nov 10, 2014
    Posts:
    63
    you didnt provide any answer to the problem.
     
  6. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Yeah he did, he told you what was causing the problem
     
    Martin_H likes this.
  7. Martin_H

    Martin_H

    Joined:
    Jul 11, 2015
    Posts:
    4,436
    I'm kind of speechless to be honest...
     
    jhocking and Schneider21 like this.
  8. skeleton-king

    skeleton-king

    Joined:
    Nov 10, 2014
    Posts:
    63
    can you people be eleborate this is begininger section.

    Incase i am not clear what i wana acheive is when i press slider_r button twice it goes to 3 and when i press slider button_l once it should go to 2 and not zero as its currently hapenning.
     
  9. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    He told you what was wrong, this is helping a beginner. He gave you the link to the tutorials, that is helping a beginner. This is not the ' write a script for someone section'. If you don't know how to fix it from what you were given then stop what you are doing & do the tutorials. Unity staff have taken the time to make them & have even done them in increasing levels of difficulty so you can learn more complex things as you progress, use them. Once you do you will be able to go back to this project & solve it yourself.
     
    Martin_H likes this.
  10. skeleton-king

    skeleton-king

    Joined:
    Nov 10, 2014
    Posts:
    63
    I have already done tutorials and i am learning if u wana dont help or talk in riddels then fck off from beginners section.
     
  11. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Read his answer, he did help you. Then maybe get yourself a better attitude
     
    Martin_H likes this.
  12. Martin_H

    Martin_H

    Joined:
    Jul 11, 2015
    Posts:
    4,436
    For real though, is he trolling us?
     
  13. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    @skeleton king These guys are just jerks. It's ridiculous that they won't make your game for you, and instead insist on pointing you towards the same resources for learning that everyone else uses. I personally feel you have what it takes to make it big in this industry, and for that, I believe you're deserving of special treatment.

    Here's the answer to your question.
     
    Ryiah, georetro and Martin_H like this.
  14. georetro

    georetro

    Joined:
    Jan 18, 2013
    Posts:
    218
    @Schneider21 Haha. So right, these goddamn n00bs are obviously not helping him!
     
    Last edited: Aug 28, 2015
  15. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    For the record, I have no issue with people being new or asking for help. What I do have a problem with is the attitude some people demonstrate when asking for help from people who are volunteering their time to assist you.
     
  16. georetro

    georetro

    Joined:
    Jan 18, 2013
    Posts:
    218
    Yeah I understand. I wasn't saying the guy who asked the question was a noob, I was playing along - I was claiming that those who were assisting him were noobs for "obviously" not helping him ;)
     
    Schneider21 likes this.
  17. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Darn Internet. Totally removing the tone from things.
     
    georetro likes this.
  18. georetro

    georetro

    Joined:
    Jan 18, 2013
    Posts:
    218
    I know right lol I'm gunna re edit my post to make sure it sounds more like what it is intended to :p
     
  19. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    temp ban.
     
Thread Status:
Not open for further replies.