using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class statroll : MonoBehaviour {
int str = 0, agi = 0, inte = 0;
public void RandomizeStats() {
for(int x = 0; x < 20; x++)
{
int statroll = Random.Range(1,4);
switch (statroll)
{
case 3:
str++;
break;
case 2:
agi++;
break;
case 1:
inte++;
break;
default:
Debug.LogError("Something went wrong, probbably with my Random.Range bounds!");
x--;
break;
}
Debug.Log("str=" + str + " :agi=" + agi + " :inte=" + inte);
}
}
}
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.
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.
Whatever happened to the thread from yesterday where I and someone else gave you what led to this code?
This one:
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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Stats : MonoBehaviour
{
//first define your stats, we make them public so that we can access them from elsewhere
public int Strength;
public int Agility;
public int Intelligence;
//Start is called when the script is first started in the game
void Start()
{
//when the Stats is first created, randomize its stats
this.RandomizeStats();
}
public void RandomizeStats()
{
//reset your stats, in the original post you wanted the minimum to be 5
this.Strength = 5;
this.Agility = 5;
this.Intelligence = 5;
const int pointsToDistribute = 20; //cause 35-15 = 20
//now loop the number of points to distribute
//distributing them to a random stat 1 at a time
for(int i = 0; i < pointsToDistribute; i++)
{
int statroll = Random.Range(1,4);
switch (statroll)
{
case 3:
this.Strength++; // ++ is short hand for += 1
break;
case 2:
this.Agility++;
break;
case 1:
this.Intelligence++;
break;
default:
Debug.LogError("Something went wrong, probbably with my Random.Range bounds!");
i--;
break;
}
}
}
}
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.
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.
Sorry, I didn’t mean I “needed” I meant I wouldn’t have made them if I knew.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class statroll : MonoBehaviour
{
//first define your stats, we make them public so that we can access them from elsewhere
public int Strength;
public int Agility;
public int Intelligence;
public Text Str;
public Button roll;
//Start is called when the script is first started in the game
void Start()
{
//when the Stats is first created, randomize its stats
RandomizeStats();
}
public void RandomizeStats()
{
//reset your stats, in the original post you wanted the minimum to be 5
this.Strength = 5;
this.Agility = 5;
this.Intelligence = 5;
const int pointsToDistribute = 20; //cause 35-15 = 20
//now loop the number of points to distribute
//distributing them to a random stat 1 at a time
for(int i = 0; i < pointsToDistribute; i++)
{
int statroll = Random.Range(1,4);
switch (statroll) {
case 3:
this.Strength++; // ++ is short hand for += 1
break;
case 2:
this.Agility++;
break;
case 1:
this.Intelligence++;
break;
default:
Debug.LogError ("Something went wrong, probbably with my Random.Range bounds!");
i--;
break;
{
Str.text = Strength.ToString();
roll.interactable = Strength < 25;
}
}
}
}
}
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
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.