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. Dismiss Notice

Adding and subtracting from a base value.

Discussion in 'Editor & General Support' started by ClawbatPokémon, Sep 27, 2014.

  1. ClawbatPokémon

    ClawbatPokémon

    Joined:
    Jun 4, 2013
    Posts:
    13
    I mean I want use 5 as a base value and using += makes it 6 and when I use -=2 from it it becomes 4.

    But the situation is different when I use += it 1becomes 6,but after I use -=1 it becomes 4 not 5.
    This is very annoying minor issue :/.
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    There's probably a simple logical error in your code.
     
  3. ClawbatPokémon

    ClawbatPokémon

    Joined:
    Jun 4, 2013
    Posts:
    13
    They are in two different methods/functions.
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    If the variable exists in each method as a local variable, they won't affect each other. If not, post a script that we can have a look at.
     
  5. ClawbatPokémon

    ClawbatPokémon

    Joined:
    Jun 4, 2013
    Posts:
    13
    Public int counter;
    {
    Public void btnr ()
    {
    If (counter > 9)
    {
    counter = 0;
    }
    spriteindex = [counter];
    counter += 1;
    }

    Public void btnl ()
    If (counter = 1)
    {
    counter = 0;
    }
    spriteindex = [counter];
    counter -= 1;
    }

    This was typed from my mobile so it may have errors.
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    There are some errors such as a missing '}' (second method) and the condition counter = 1 in the second method will always be true. Not sure whether it's a typo as you said you're posting from the mobile phone, but it has to be '=='.
    Also, the second method does not make alot of sense to me, what are you trying to achieve with those?
     
  7. ClawbatPokémon

    ClawbatPokémon

    Joined:
    Jun 4, 2013
    Posts:
    13
    If you press the right button it changes to the next sprite in the array and if you press the left button it changes to a previous sprite.

    And when the counter reaches maximum/minimum value interests itself.

    9 max and 1 min
     
  8. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,952
    There are several issues with the code you provided. Assuming is because you typed it in via a phone, I won't worry about the index stuff. Basically cleaned up it should look something like this:
    Code (CSharp):
    1.     public int counter;
    2.     public void btnr ()
    3.     {
    4.         counter += 1;
    5.         if (counter > 9)
    6.         {
    7.             counter = 0;
    8.         }
    9.     }
    10.  
    11.     public void btnl ()
    12.     {
    13.         counter -= 1;
    14.         if (counter < 0)
    15.         {
    16.             counter = 9;
    17.         }
    18.     }
    This assumes you are stepping through an array, so the values would be 0-9. You inc/dec the counter then wrap it.
    Though for something like this I usually use a ternary or "?" operator.
    Code (CSharp):
    1.  
    2.     public int counter;
    3.     public int counter_max = 9;
    4.  
    5.     public void btnr ()
    6.     {
    7.         counter = (counter+1 > counter_max) ? 0 : counter+1;
    8.     }
    9.  
    10.     public void btnl ()
    11.     {
    12.         counter = (counter-1 < 0) ? counter_max : counter-1;
    13.     }
     
  9. ClawbatPokémon

    ClawbatPokémon

    Joined:
    Jun 4, 2013
    Posts:
    13
    your code is okay but it still doesn't produce the results I want.

    when I press left button it changes sprite 3 to sprite 2, but after I press the right button it changes from sprite 3 to 4 instead. This is my issue.
     
  10. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Post the updated code with code tags (copy/paste, please not type it from mobile again).
    The code provided by @zombiegorilla looks correct to me.
     
  11. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,952
    It sounds like you are not changing the base value then. As @Suddoha suggested, please paste your complete code.
     
  12. ClawbatPokémon

    ClawbatPokémon

    Joined:
    Jun 4, 2013
    Posts:
    13
    using UnityEngine;
    using System.Collections;

    public class imagechanger : MonoBehaviour {

    // Use this for initialization
    public Sprite[] sprites;
    public GameObject mouth;
    public SpriteRenderer spriteRenderer;
    public int counter;
    public void clickpos()
    {
    switch(counter)
    { case 0:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 1:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 2:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 3:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 4:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 5:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 6:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 7:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 8:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 9:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 10:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 11:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 12:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 13:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 14:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 15:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 16:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 17:
    counter += 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    case 18:
    counter = 0;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    break;
    }
    }

    public void clickneg()
    {
    counter -= 1;
    spriteRenderer.sprite = sprites[counter];
    Debug.Log(counter);
    }
    }

    Code which I wrote.
     
  13. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,952
    First, please use code tags.

    Not sure why you are using a switch, this will do the trick:
    Code (CSharp):
    1.     public SpriteRenderer spriteRenderer;
    2.     public Sprite[] sprites;
    3.     public int counter;
    4.  
    5.     public void clickpos()
    6.     {
    7.         counter = (counter+1 < sprites.Length) ? counter+1 : 0;
    8.         spriteRenderer.sprite = sprites[counter];
    9.         Debug.Log(counter);
    10.     }
    11.  
    12.  
    13.     public void clickneg()
    14.     {
    15.         counter = (counter-1 >= 0) ? counter-1 : sprites.Length-1;
    16.         spriteRenderer.sprite = sprites[counter];
    17.         Debug.Log(counter);
    18.     }
    Edit: Array of sprites not strings.
     
    Last edited: Oct 11, 2014
  14. ClawbatPokémon

    ClawbatPokémon

    Joined:
    Jun 4, 2013
    Posts:
    13
    It still doesn't show the result I want, when the value is 4 I press the positive arrow it becomes 5, but when I press the negative arrow it becomes 1, I want it to be 4 instead.
     
  15. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,952
    That code snippet works as expected. Something else is going on that is affecting it. Are you setting the counter elsewhere? Are key strokes counting more than once? Are you only using the code in one place? are you changing the array elsewhere?

    Please post your complete script as you have it in the game. (in code tags). I am guessing something else it affecting the counter value.
     
  16. ClawbatPokémon

    ClawbatPokémon

    Joined:
    Jun 4, 2013
    Posts:
    13
    I got it I think so, I'm using the same script on two different objects,instead I should call the method from a single script using getComponent I suppose.

    I'll try it and post updates.
     
  17. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,952
    Heh... that would definitely cause the problem you are seeing. Sadly, I have done that same thing on more than one occasion, trying to debug a script, when the problem was it was attached in the wrong place or duplicated.