Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need help with randomly generated numbers equaling to 35.

Discussion in 'Scripting' started by Enderkill9, Apr 29, 2017.

  1. Enderkill9

    Enderkill9

    Joined:
    Apr 26, 2017
    Posts:
    39
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class statroll : MonoBehaviour {
    7.     int str = 0, agi = 0, inte = 0;
    8.  
    9.     public void RandomizeStats() {
    10.  
    11.     for(int x = 0; x < 20; x++)
    12.     {
    13.         int statroll = Random.Range(1,4);
    14.         switch (statroll)
    15.         {
    16.         case 3:
    17.             str++;
    18.             break;
    19.         case 2:
    20.             agi++;
    21.             break;
    22.         case 1:
    23.             inte++;
    24.             break;
    25.         default:
    26.             Debug.LogError("Something went wrong, probbably with my Random.Range bounds!");
    27.             x--;
    28.             break;
    29.         }
    30.             Debug.Log("str=" + str + " :agi=" + agi + " :inte=" + inte);
    31.  
    32.     }
    33.  
    34. }
    35. }
    36.  
    37.  
    I'm very close to getting this to work how I want it to. What I'm trying to do is when I press a button. Three numbers are randomly generated. What I can't get to work is to have these three numbers to equal to 35, because when I press the button the numbers are generated, but they don't equal to 35, their values just keep increasing. I want it so everytime I press the button the three numbers equal 35.
     
  2. UziMonkey

    UziMonkey

    Joined:
    Nov 7, 2012
    Posts:
    206
    You need to reset str, agi and inte at the beginning of RandomizeStats. Also, you probably want that counter to go up to 35, instead of 20. Or else add 5 to each stat after 20 rounds.
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Wow, man. I just responded to .. one of a half dozen of these threads..
    OP, you are a bit overkill on the new threads :)
     
  4. Enderkill9

    Enderkill9

    Joined:
    Apr 26, 2017
    Posts:
    39
    Thanks for your help.
     
  5. Enderkill9

    Enderkill9

    Joined:
    Apr 26, 2017
    Posts:
    39
    I know I should've waited for your response, sorry.
     
  6. Enderkill9

    Enderkill9

    Joined:
    Apr 26, 2017
    Posts:
    39
    I appreciate the fact that you take your time to respond to my threads, I barely even know how to program so thanks.
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It's all good - have fun with your game :)
     
  8. Enderkill9

    Enderkill9

    Joined:
    Apr 26, 2017
    Posts:
    39
    How exactly would I reset the variables?
     
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    Whatever happened to the thread from yesterday where I and someone else gave you what led to this code?

    This one:
    https://forum.unity3d.com/threads/rpg-rolling-for-stats.468616/#post-3051329

    OK... so you're having a lot of issues here with the fundamentals, yet you're still focusing on the generating numbers upto 35.

    So here, I'm going to break it down:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Stats : MonoBehaviour
    7. {
    8.     //first define your stats, we make them public so that we can access them from elsewhere
    9.     public int Strength;
    10.     public int Agility;
    11.     public int Intelligence;
    12.  
    13.     //Start is called when the script is first started in the game
    14.     void Start()
    15.     {
    16.         //when the Stats is first created, randomize its stats
    17.         this.RandomizeStats();
    18.     }
    19.  
    20.     public void RandomizeStats()
    21.     {
    22.         //reset your stats, in the original post you wanted the minimum to be 5
    23.         this.Strength = 5;
    24.         this.Agility = 5;
    25.         this.Intelligence = 5;
    26.         const int pointsToDistribute = 20; //cause 35-15 = 20
    27.    
    28.         //now loop the number of points to distribute
    29.         //distributing them to a random stat 1 at a time
    30.         for(int i = 0; i < pointsToDistribute; i++)
    31.         {
    32.             int statroll = Random.Range(1,4);
    33.             switch (statroll)
    34.             {
    35.                 case 3:
    36.                     this.Strength++; // ++ is short hand for += 1
    37.                     break;
    38.                 case 2:
    39.                     this.Agility++;
    40.                     break;
    41.                 case 1:
    42.                     this.Intelligence++;
    43.                     break;
    44.                 default:
    45.                     Debug.LogError("Something went wrong, probbably with my Random.Range bounds!");
    46.                     i--;
    47.                     break;
    48.             }
    49.         }
    50.  
    51.     }
    52. }
    53.  
    Next time, stop creating so many threads. Stick to the thread that deals with your question so that other people understand the history that got you to the point you're at.
     
  10. Enderkill9

    Enderkill9

    Joined:
    Apr 26, 2017
    Posts:
    39
    Finally, this is all I needed. If you would have showed me this in the first place I wouldn't have needed to make all these threads, so thanks.
     
  11. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    Well... you didn't need to make all the threads either way.

    You made all the threads unnecessarily.

    That'd be like saying you wouldn't have needed to murder all those children if I had just locked you up yesterday... no, you wouldn't have been able to. But no 'need' ever existed.

    Unless you're suggesting you were extorting assistance by pestering the community.... in which case, you don't just have a bit of programming knowledge ahead of you, but also forum etiquette training.
     
  12. UziMonkey

    UziMonkey

    Joined:
    Nov 7, 2012
    Posts:
    206
    That confusion could have been avoided by not making more than one thread. You only ever need one thread, if no one answers you right away, just wait.
     
  13. Enderkill9

    Enderkill9

    Joined:
    Apr 26, 2017
    Posts:
    39
    Sorry, I didn't mean I "needed" I meant I wouldn't have made them if I knew.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class statroll : MonoBehaviour
    7. {
    8.     //first define your stats, we make them public so that we can access them from elsewhere
    9.     public int Strength;
    10.     public int Agility;
    11.     public int Intelligence;
    12.     public Text Str;
    13.     public Button roll;
    14.  
    15.     //Start is called when the script is first started in the game
    16.     void Start()
    17.     {
    18.         //when the Stats is first created, randomize its stats
    19.         RandomizeStats();
    20.     }
    21.  
    22.     public void RandomizeStats()
    23.     {
    24.         //reset your stats, in the original post you wanted the minimum to be 5
    25.         this.Strength = 5;
    26.         this.Agility = 5;
    27.         this.Intelligence = 5;
    28.         const int pointsToDistribute = 20; //cause 35-15 = 20
    29.  
    30.         //now loop the number of points to distribute
    31.         //distributing them to a random stat 1 at a time
    32.         for(int i = 0; i < pointsToDistribute; i++)
    33.         {
    34.             int statroll = Random.Range(1,4);
    35.             switch (statroll) {
    36.             case 3:
    37.                 this.Strength++; // ++ is short hand for += 1
    38.                 break;
    39.             case 2:
    40.                 this.Agility++;
    41.                 break;
    42.             case 1:
    43.                 this.Intelligence++;
    44.                 break;
    45.             default:
    46.                 Debug.LogError ("Something went wrong, probbably with my Random.Range bounds!");
    47.                 i--;
    48.                 break;
    49.                 {
    50.                     Str.text = Strength.ToString();
    51.  
    52.                     roll.interactable = Strength < 25;
    53.    
    54.                 }
    55.             }
    56.         }
    57.  
    58.     }
    59. }
    60.  
    If you're still willing to help me after all that, I'm trying to make a public button (roll) and public text (str) so I can insert the text that the code is going to be changing. I'm only trying it on strength so far just for testing purposes. The problem I'm having is that no matter what I do, the text never changes when I press the roll button. I know I have been annoying with all the threads, so I would understand if you didn't care about helping me
     
  14. UziMonkey

    UziMonkey

    Joined:
    Nov 7, 2012
    Posts:
    206
    Lines 49 to 54 are not reachable, did you intend these to be inside the switch statement? If not, remove the braces from around them and move them after the closing brace for the switch statement.
     
  15. Enderkill9

    Enderkill9

    Joined:
    Apr 26, 2017
    Posts:
    39
    Wow! Thank you!