Ok so I am following a tutorial on youtube for making an rpg. In the video they have created script that gives static creation stats, what I’m wanting to do when the character creation process is started I’m wanting the few stats that they will have be randomized between set numbers. Here is what I have right now for the character stats:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BaseWarriorClass : BaseCharacterClass
{
public BaseWarriorClass()
{
CharacterClassName = "Warrior";
CharacterClassDescription = "A sword wielding hero!";
Strength = 10;
Damage = 8;
Stamina = 5;
Intelligence = 3;
}
}
As you can see right now the given stats have defined values but I want it to be where the strength could be anything between let’s say 7 and 10. So I tried doing this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BaseWarriorClass : BaseCharacterClass
{
public BaseWarriorClass()
{
CharacterClassName = "Warrior";
CharacterClassDescription = "A sword wielding hero!";
Strength = Random.Range(7,11);
Damage = 8;
Stamina = 5;
Intelligence = 3;
}
}
And get the following error in unity:
UnityException: RandomRangeInt is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour ‘TestGUI’ on game object ‘Main Camera’.
See “Script Serialization” page in the Unity Manual for further details.
UnityEngine.Random.Range (System.Int32 min, System.Int32 max) (at C:/buildslave/unity/build/Runtime/Export/Random.bindings.cs:48)
BaseWarriorClass…ctor () (at Assets/Scripts/Character Class/BaseWarriorClass.cs:12)
TestGUI…ctor () (at Assets/Scripts/TestGUI.cs:8)
With me being new to all this I’m not sure what I need to do to achieve what I’m wanting and do not understand the error. If there is more information needed let me know and I’ll do what I can to give what is needed.
Your BaseWarriorClass inherits BaseCharacterClass. BaseCharacterClass probably iherits MonoBehaviour because all unity scripts inherits MonoBehaviour. Therefore, your BaseWarriorClass is MonoBehaviour. You invoked Random.Range method from the BaseWarriorClass constructor. The error message says you must invoke it from the Start or Awake methods instead of constructor, like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BaseWarriorClass : BaseCharacterClass
{
public BaseWarriorClass()
{
CharacterClassName = "Warrior";
CharacterClassDescription = "A sword wielding hero!";
Damage = 8;
Stamina = 5;
Intelligence = 3;
}
public void Awake() {
Strength = Random.Range(7,11);
}
}
Ok so I tried this with my stats and it’s giving back all 0’s.
Here is what I have now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BaseWarriorClass : BaseCharacterClass
{
public BaseWarriorClass()
{
CharacterClassName = "Warrior";
CharacterClassDescription = "A sword wielding hero!";
//Strength = 10;
//Damage = 8;
//Stamina = 5;
//Intelligence = 3;
}
public void Awake()
{
Strength = Random.Range(7, 11);
Damage = Random.Range(6, 10);
Stamina = Random.Range(4, 8);
Intelligence = Random.Range(0, 5);
}
}
Here is the BaseCharacterClass that the BaseWarriorClass is inheriting from:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BaseCharacterClass {
private string characterClassName;
private string characterClassDescription;
//stats
private int stamina;
private int damage;
private int strength;
private int intelligence;
public string CharacterClassName { get; set; }
public string CharacterClassDescription { get; set; }
public int Stamina { get; set; }
public int Damage { get; set; }
public int Strength { get; set; }
public int Intelligence { get; set; }
}
Your properties are not connected to the class fields.
Wrong:
public class BaseCharacterClass {
private int stamina;
public int Stamina { get; set; }
}
This code defines two places to store 2 values not related each to other: manually declared stamina field and autogenerated backing field for Stamina property. Changing one of them won;t affect another.
Right:
public class BaseCharacterClass {
private int stamina;
public int Stamina {
get { return stamina; }
set { stamina = value; }
}
}
In this code there is one value - stamina, wich can be accesed (read & write) through Stamina property.
The BaseCharacterClass is also not a MonoBehaviour, which means Awake() will never be called. In this case, I would actually not use the Unity Random class. There is no obvious reason for your class to be a MonoBehaviour, so use built in C# Random class instead as Brathnann suggests.
Random rand = new Random();
var stat = rand.Next(7,11);
Ok, so I believe I have updated/adjusted as suggested. If not I do apologize I am clearly very new to unity and C#.
Here is the new scripts:
public class BaseCharacterClass {
private string characterClassName;
private string characterClassDescription;
//stats
private int stamina;
private int damage;
private int strength;
private int intelligence;
public string CharacterClassName { get; set; }
public string CharacterClassDescription { get; set; }
public int Stamina
{
get { return stamina; }
set { stamina = value; }
}
public int Damage
{
get { return damage; }
set { damage = value; }
}
public int Strength
{
get { return strength; }
set { strength = value; }
}
public int Intelligence
{
get { return intelligence; }
set { intelligence = value; }
}
}
And:
using UnityEngine;
public class BaseWarriorClass : BaseCharacterClass
{
public BaseWarriorClass()
{
CharacterClassName = "Warrior";
CharacterClassDescription = "A sword wielding hero!";
Random rand = new Random();
var Strength = rand.Next(7, 11);
Random rand1 = new Random();
var Damage = rand.Next(6, 10);
Random rand2 = new Random();
var Stamina = rand.Next(4, 8);
Random rand3 = new Random();
var Intelligence = rand.Next(0, 5);
}
}
I am now getting the following:
Error CS1061 ‘Random’ does not contain a definition for ‘Next’ and no accessible extension method ‘Next’ accepting a first argument of type ‘Random’ could be found (are you missing a using directive or an assembly reference?)
Not sure if it makes a difference but I’m using Visual Studio Ver. 15.9.11 and Unity 2018.3.12f1
using UnityEngine;
public class BaseWarriorClass : BaseCharacterClass
{
public BaseWarriorClass()
{
CharacterClassName = "Warrior";
CharacterClassDescription = "A sword wielding hero!";
var rand = new System.Random(); //Or System.Random rand = new System.Random();
var Strength = rand.Next(7, 11);
var Damage = rand.Next(6, 10);
var Stamina = rand.Next(4, 8);
var Intelligence = rand.Next(0, 5);
}
}
I had to make a slight change to what your suggested code was to get it to work.
But it is now working for me. Thank you for your assistance!
public BaseWarriorClass()
{
CharacterClassName = "Warrior";
CharacterClassDescription = "A sword wielding hero!";
var rand = new System.Random();
Strength = rand.Next(7, 11);
Damage = rand.Next(6, 10);
Stamina = rand.Next(4, 8);
Intelligence = rand.Next(0, 5);
}
However I did notice that I couldn’t have the same range for the same stat on multiple classes because they would show the same results.
So that is what I’m going to work on next. I’m assuming if I was to change the names slightly to represent each class then it would allow the generation of different numbers instead of just “Strength” maybe change it to “WarStrength” for my warrior and maybe “WmStrength” for my white mage ect.
EDIT: So, that didn’t work. Even tried changing the var name and that didn’t work either.