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

Need help! Can`t understand!

Discussion in 'Scripting' started by Ants0li, Nov 29, 2014.

  1. Ants0li

    Ants0li

    Joined:
    Nov 29, 2014
    Posts:
    12
    Hello everybody! I have a big problem.
    So, my main task is to change speed of my char if score equals to 100.
    So i have few scripts for it.

    First of all im sending scores to my movement script
    Code (csharp):
    1.  
    2. void OnDisable()
    3.     {
    4.         PlayerPrefs.SetInt ("Score", (int)playerScore);
    5.     }
    Then im taking scores in my movement script and creating if statement, that if scores equals to 100 speed will become +1 from original speed ( its equals to 3.0f)
    Code (csharp):
    1.  
    2.  
    3. void increasespeed()
    4.     {
    5.         score = PlayerPrefs.GetInt ("Score") * 10;
    6.         if((int)score == 100)
    7.         {
    8.             speed = speed + 1.0f;
    9.         }
    10.     }
    But char speed is staying 3.0 without any change
    Where`s my mistake?
     
    Last edited: Nov 29, 2014
  2. zRedCode

    zRedCode

    Joined:
    Nov 11, 2013
    Posts:
    131
    Maybe you score variable never get the 100 value, and the if never starts.
    You can try adding a line like:
    Code (CSharp):
    1. Debug.Log(score, gameobject);
    in the function increaseSpeed() and execute the game, checking the console, it will say you the value of score. Or using breakpoints for debugging.
     
  3. Ants0li

    Ants0li

    Joined:
    Nov 29, 2014
    Posts:
    12
    I don`t know how does it works, i`m just added this line in my code, but nothing happens. Doesn`t matter, i`ve checked my "Score-system" it works totally correctly.
    Also, system which send score-info is working too. I don`t have any ideas.
     
  4. zRedCode

    zRedCode

    Joined:
    Nov 11, 2013
    Posts:
    131
    Basic question: do you call that function in Update?
     
  5. Ants0li

    Ants0li

    Joined:
    Nov 29, 2014
    Posts:
    12
    Uhm... Maybe i`m so newbie at this, but i can`t understand: is it necessarily?
    No, i don`t. How can i do it?
     
  6. Ants0li

    Ants0li

    Joined:
    Nov 29, 2014
    Posts:
    12
    Okay, i`ve added if statement in Update.
    If i keep *10 (score) nothing happens, but if i will change it to 100 my char going to fly away.
     
  7. zRedCode

    zRedCode

    Joined:
    Nov 11, 2013
    Posts:
    131
    Can you post the full script?
     
  8. Ants0li

    Ants0li

    Joined:
    Nov 29, 2014
    Posts:
    12
    I have few scripts in my game. Maybe i can upload full project for you? Or you need just these scripts ( score and movement ) ?
     
  9. zRedCode

    zRedCode

    Joined:
    Nov 11, 2013
    Posts:
    131
    i need the class where is the code posted in the open post.
     
  10. Ants0li

    Ants0li

    Joined:
    Nov 29, 2014
    Posts:
    12
    I`m using two.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class cmove : MonoBehaviour {
    6.    
    7.     public float speed = 6.0f; // Player`s speed
    8.     Transform groundcheck;
    9.     private float overlapRadius = 0.2f;
    10.     public LayerMask whatIsGround;
    11.     private bool grounded = false;
    12.     private bool jump = false;
    13.     public float jumpforce = 60f;
    14.     private bool doublejump = false;
    15.     float score = 0;
    16.    
    17.  
    18.     void Start()
    19.     {
    20.         score = PlayerPrefs.GetInt ("Score") * 10;
    21.         groundcheck = transform.Find ("groundcheck");
    22.     }
    23.    
    24.     void Update()
    25.     {
    26.         if(score>=120)
    27.         {
    28.             speed = speed + 1f;
    29.         }
    30.  
    31.  
    32.         if (Input.GetKeyDown(KeyCode.Space))
    33.             jump = true;
    34.     }
    35.    
    36.     void FixedUpdate()
    37.     {
    38.        
    39.         // Check char grounded
    40.        
    41.         grounded = Physics2D.OverlapCircle(groundcheck.position, overlapRadius, whatIsGround);
    42.         // Reseting doublejump when on the ground  
    43.        
    44.         if (grounded)
    45.             doublejump = false;
    46.        
    47.         // Determine if player can jump
    48.         bool canJump = (grounded || !doublejump);
    49.        
    50.         //
    51.        
    52.         if (jump && canJump)
    53.         {
    54.             rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
    55.             rigidbody2D.AddForce(new Vector2(0, jumpforce));
    56.            
    57.            
    58.             if (!grounded)
    59.                 doublejump = true;
    60.         }
    61.        
    62.         jump = false;
    63.        
    64.         rigidbody2D.velocity = new Vector2(speed, rigidbody2D.velocity.y); // const speed
    65.     }
    66. }

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ScoreScript : MonoBehaviour {
    6.  
    7.     float playerScore = 0;
    8.  
    9.     void Update () {
    10.         playerScore += Time.deltaTime;
    11.  
    12.     }
    13.     public void IncreaseScore(int amount)
    14.     {
    15.         playerScore += amount;
    16.     }
    17.  
    18.     void OnDisable()
    19.     {
    20.         PlayerPrefs.SetInt ("Score", (int)playerScore);
    21.     }
    22.  
    23.     void OnGUI()
    24.     {
    25.         GUI.Label (new Rect (10, 10, 100, 30), "Score: " + (int)(playerScore * 10));
    26.     }
    27.  
    28.  
    29.  
    30. }
    31.  
     
  11. zRedCode

    zRedCode

    Joined:
    Nov 11, 2013
    Posts:
    131
    Never used an OOP language?
    The Function IncreaseScore isn't called on the attached script, so the code will never runned. There is a class that calls that function? If not, thats why the code not works, if yes, post that class.
     
  12. Ants0li

    Ants0li

    Joined:
    Nov 29, 2014
    Posts:
    12
    uhm... I think i have some big troubles in my knowledge.
    What should i do?
    Should i create public, void or something another?
     
  13. zRedCode

    zRedCode

    Joined:
    Nov 11, 2013
    Posts:
    131
    no, you not have to create anything, simply call the function. A void is a type of function. Just look this video, it explains variables and functions.
     
  14. Ants0li

    Ants0li

    Joined:
    Nov 29, 2014
    Posts:
    12
    I can`t understand what i should change. I have some void ( which is function ), i have added something to Update void. I can`t understand anyway.
     
  15. zRedCode

    zRedCode

    Joined:
    Nov 11, 2013
    Posts:
    131
    To call a function you should write in a called function
    Code (CSharp):
    1. FunctionName(parameters);
    For example:
    Code (CSharp):
    1. void Update()
    2. {
    3.   //calling the FunctionA function
    4.   FunctionA();
    5. }
    if you still not understand i suggest to follow all the scripting tutorials in the learn section of unity3d.com
     
  16. Ants0li

    Ants0li

    Joined:
    Nov 29, 2014
    Posts:
    12
    Oh yeah, my fail. Sure, i can understand you now.
    I changed my code a bit, but it still doesnt work.

    Now i have something like that:
    Code (csharp):
    1. public float baseSpeed = 3.0f;
    2.     public float speed = 0;
    3.     int score = 0;
    4.  
    5.     void IncreaseSpeed()
    6.     {
    7.         speed = baseSpeed + (int)score/100;
    8.     }
    9.  
    10.     void Start()
    11.     {
    12.         groundcheck = transform.Find ("groundcheck");
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         IncreaseSpeed();
    18.         score = PlayerPrefs.GetInt ("Score") * 10;
    19.         if (Input.GetKeyDown(KeyCode.Space))
    20.             jump = true;
    21.  
    22.     }
     
  17. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Maybe move this line
    Code (csharp):
    1. score = PlayerPrefs.GetInt ("Score") * 10;
    from the Start function to the Update function
     
  18. Ants0li

    Ants0li

    Joined:
    Nov 29, 2014
    Posts:
    12
    Yes, i moved it to Update
     
  19. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    True, didn't see that... What is the GUILabel of the score showing...
     
  20. Ants0li

    Ants0li

    Joined:
    Nov 29, 2014
    Posts:
    12
    First label is for GAME OVER title, second is for the total score, and the last button is for RETRY fuction.
     
  21. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Yeah what value is this showing
    Code (csharp):
    1. GUI.Label (new Rect (10, 10, 100, 30), "Score: " + (int)(playerScore * 10));
    when you run it
     
  22. Ants0li

    Ants0li

    Joined:
    Nov 29, 2014
    Posts:
    12
    Oh, this is my mistake, sorry. It doesnt show my anything
     
  23. zRedCode

    zRedCode

    Joined:
    Nov 11, 2013
    Posts:
    131
    create a boolean variable, assign then a value true and change that gui line like:
    Code (CSharp):
    1. BoolVariableName = GUI.Label(new rect(10, 10, 100, 30), "Score: " + (int)(playerScore * 10 ));
    And put this in the OnGUI function.
    Also use the new UI in unity 4.6 instead of the old gui that is deprecated.