I am getting the error
GUI Error: You are pushing more GUIClips than you are popping. Make sure they are balanced)
and the error
ArgumentException: The prefab you want to instantiate is null.
UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:104)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:83)
PC.get_Instance () (at Assets/Scripts/Character/PC.cs:20)
CharacterGenerator.DisplayName () (at Assets/Scripts/Character/CharacterGenerator.cs:94)
CharacterGenerator.MyWindow (Int32 id) (at Assets/Scripts/Character/CharacterGenerator.cs:79)
UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/GUI.cs:1402)
This is the script I have attached to my main camera.
/// <summary>
/// CharacterGenerator.cs
/// Aug 19, 2014
/// Uncle Jimz
///
/// This script is used to help the user generate a character.
///
using UnityEngine;
using System.Collections;
using System; //used for the Enum class
public class CharacterGenerator : MonoBehaviour {
// private PlayerCharacter _toon;
private const int STARTING_POINTS = 350;
private const int MIN_STARTING_ATTRIBUTE_VALUE = 10;
private const int STARTING_VALUE = 50;
private int pointsLeft;
private const int OFFSET = 5;
private const int LINE_HEIGHT = 32;
private const int STAT_LABEL_WIDTH = 200;
private const int BASEVALUE_LABEL_WIDTH = 90;
private const int BUTTON_WIDTH = 60;
private const int BUTTON_HEIGHT = 32;
private Rect windowRect;
private int statStartingPos = 30;
public GUISkin mySkin;
public float delayTimer = .25f;
private float _lastClick = 0;
void Awake() {
// Debug.Log("***CharacterGenerator - Awake***");
// PC.Instance.Initialize();
}
// Use this for initialization
void Start () {
// Debug.Log("***CharacterGenerator - Start***");
pointsLeft = STARTING_POINTS;
for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
PC.Instance.GetPrimaryAttribute(cnt).BaseValue = STARTING_VALUE;
pointsLeft -= (STARTING_VALUE - MIN_STARTING_ATTRIBUTE_VALUE);
}
PC.Instance.StatUpdate();
}
//update the GUI
void OnGUI() {
GUI.skin = mySkin;
windowRect = GUI.Window( 0, windowRect, MyWindow, "Character Creation" );
// if(_toon.Name == "" || pointsLeft > 0)
// DisplayCreateLabel();
// else
// DisplayCreateButton();
}
public void Update() {
windowRect = new Rect( -5, -23, Screen.width + 10, Screen.height + 46 );
}
private void MyWindow( int id ) {
DisplayName();
DisplayPointsLeft();
DisplayAttributes();
DisplayVitals();
DisplaySkills();
if(PC.Instance.name != "" && pointsLeft < 1)
DisplayCreateButton();
}
//display the name lable as well as the textbox for them to enter the name
private void DisplayName() {
GUI.BeginGroup( new Rect( 40, 90, 400, LINE_HEIGHT ) );
GUI.Label(new Rect( 0, 0, 120, LINE_HEIGHT), "Name:");
PC.Instance.name = GUI.TextField(new Rect(120, 0, 200, LINE_HEIGHT), PC.Instance.name);
GUI.EndGroup();
}
//display all of the attributes as well as the +/- boxes for the user to alter the values
private void DisplayAttributes() {
GUI.BeginGroup(new Rect( 40, 90 + LINE_HEIGHT, ( Screen.width - 80 ) / 2, LINE_HEIGHT * ( Enum.GetValues(typeof(AttributeName)).Length + 1 ) ), "Attributes", "box" );
for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
GUI.Label(new Rect( OFFSET, //x
statStartingPos + (cnt * LINE_HEIGHT), //y
STAT_LABEL_WIDTH, //width
LINE_HEIGHT //height
), ((AttributeName)cnt).ToString());
GUI.Label(new Rect( STAT_LABEL_WIDTH + OFFSET, //x
statStartingPos + (cnt * LINE_HEIGHT), //y
BASEVALUE_LABEL_WIDTH, //width
LINE_HEIGHT //height
), PC.Instance.GetPrimaryAttribute(cnt).AdjustedBaseValue.ToString());
if(GUI.RepeatButton(new Rect( OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH, //x
statStartingPos + (cnt * BUTTON_HEIGHT), //y
BUTTON_WIDTH, //width
BUTTON_HEIGHT //height
), "-")) {
if(Time.time - _lastClick > delayTimer) {
if(PC.Instance.GetPrimaryAttribute(cnt).BaseValue > MIN_STARTING_ATTRIBUTE_VALUE) {
PC.Instance.GetPrimaryAttribute(cnt).BaseValue--;
pointsLeft++;
PC.Instance.StatUpdate();
}
_lastClick = Time.time;
}
}
if(GUI.RepeatButton(new Rect( OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH + BUTTON_WIDTH, //x
statStartingPos + (cnt * BUTTON_HEIGHT), //y
BUTTON_WIDTH, //width
BUTTON_HEIGHT //height
), "+")) {
if(Time.time - _lastClick > delayTimer) {
if(pointsLeft > 0) {
PC.Instance.GetPrimaryAttribute(cnt).BaseValue++;
pointsLeft--;
PC.Instance.StatUpdate();
}
_lastClick = Time.time;
}
}
}
GUI.EndGroup();
}
//display all of the vitals
private void DisplayVitals() {
GUI.BeginGroup(new Rect( 40, 90 + LINE_HEIGHT * (Enum.GetValues(typeof(AttributeName)).Length + 2), ( Screen.width - 80 ) / 2, LINE_HEIGHT * ( Enum.GetValues(typeof(VitalName)).Length + 1 ) ), "Vitals", "box" );
for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++) {
GUI.Label(new Rect( OFFSET, //x
statStartingPos + (cnt * LINE_HEIGHT), //y
STAT_LABEL_WIDTH, //width
LINE_HEIGHT //height
), ((VitalName)cnt).ToString());
GUI.Label(new Rect( OFFSET + STAT_LABEL_WIDTH, //x
statStartingPos + (cnt * LINE_HEIGHT), //y
BASEVALUE_LABEL_WIDTH, //width
LINE_HEIGHT //height
), PC.Instance.GetVital(cnt).AdjustedBaseValue.ToString());
}
GUI.EndGroup();
}
//display all of the skills
private void DisplaySkills() {
GUI.BeginGroup(new Rect( Screen.width - ( Screen.width - 20 ) / 2, 90 + LINE_HEIGHT, ( Screen.width - 80 ) / 2, LINE_HEIGHT * ( Enum.GetValues(typeof(SkillName)).Length + 1 ) ), "Attributes", "box" );
for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++) {
GUI.Label(new Rect( 0, //x
statStartingPos + (cnt * LINE_HEIGHT), //y
STAT_LABEL_WIDTH, //width
LINE_HEIGHT //height
), ((SkillName)cnt).ToString());
GUI.Label(new Rect( STAT_LABEL_WIDTH, //x
statStartingPos + (cnt * LINE_HEIGHT), //y
BASEVALUE_LABEL_WIDTH, //width
LINE_HEIGHT //height
), PC.Instance.GetSkill(cnt).AdjustedBaseValue.ToString());
}
GUI.EndGroup();
}
//show them how many points they have left to spend
private void DisplayPointsLeft() {
GUI.Label(new Rect(Screen.width - 150 - 30, 90, 150, LINE_HEIGHT), "Points Left: " + pointsLeft.ToString());
}
//display label that looks like a button until they have all the requiements filled out to create a character
private void DisplayCreateLabel() {
// GUI.Label(new Rect( Screen.width/ 2 - 50, statStartingPos + (10 * LINE_HEIGHT), 100, LINE_HEIGHT), "Creating...", "Button");
}
//display the button that will call the save method when pressed, and then load the first level
private void DisplayCreateButton() {
if(GUI.Button(new Rect( Screen.width/ 2 - 50, statStartingPos + (10 * LINE_HEIGHT), 100, LINE_HEIGHT), "Next")) {
//change the cur value of the vitals to the max modified value of that vital
UpdateCurVitalValues();
GameSetting2.SaveName( PC.Instance.name );
GameSetting2.SaveAttributes( PC.Instance.primaryAttribute );
GameSetting2.SaveVitals( PC.Instance.vital );
GameSetting2.SaveSkills( PC.Instance.skill );
Application.LoadLevel(GameSetting2.levelNames[2]);
}
}
//update the curValue for each vital to be the max value it can be so we do not start with a zero in any vital
private void UpdateCurVitalValues() {
for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++) {
PC.Instance.GetVital(cnt).CurValue = PC.Instance.GetVital(cnt).AdjustedBaseValue;
}
}
}
Cherno, I am still getting used to using Unity so can you please explain what you mean?
– NathanOf course! Each time you use GUI.BeginGroup, you also have to use GUI.EndGroup() in the same function. It works like parentheses (), so they can be nested like ((())), but every start has to have and end. You can comment out code with /* code bit to be commented out */ and //line of code to be commented out to deactive parts of your code, and find the error part.
– CornetcyDouble click on the error message in the console and it takes you to the offending line in question. It is probably this one, which I have no idea what it does: PC.Instance.GetPrimaryAttribute(cnt).BaseValue = STARTING_VALUE;
– CornetcyWOW I had no idea you could do that! That seems to have fixed all errors. Thank you so much for your tyme!
– NathanDe nada, Glad I could help! Happy coding!
– Cornetcy