I have one box in the scene and when the box is trigger by the player, I want to add a text to the GUILayout.Label. Last time, I got this following error: ArgumentException: Getting control 0’s position in a group with only 0 controls when doing Repaint. But right now it is solved (no error shown), but the label is adding to the GUILayout.Label every single frame.
Here is the code that I am using:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class QuestManager : MonoBehaviour
{
public static bool acceptedChiefQuest = false; // Define the boolean for the quest acception
private List<string> someTexts = new List<string>(); // Define the List of string for the quests
private Vector2 scrollPosition = Vector2.zero; // Define the Vector2 for the vertical scroll bar
private Rect windowRect = new Rect(); // Define the Rect for the window that holds the quests
private GUIStyle style = null; // Define the GUIStyle for the style that use by window quests
public AudioClip audioSEClip = null; // Define the AudioClip for the audio whenever the quest is added
private void Start()
{
// Define the position of the Rect
windowRect = GameManager.WindowScreen(Screen.width - 350, 365);
// Add the quest
AddingQuest("Talk to the chief");
}
private void SetStyle()
{
// Set the style
style = new GUIStyle(GUI.skin.label);
// Set the text alignment
style.alignment = TextAnchor.MiddleLeft;
// Set the word wrap
style.wordWrap = true;
// Set the font style
style.fontStyle = FontStyle.BoldAndItalic;
// Set the font size
style.fontSize = 11;
}
private void OnGUI()
{
// Call the SetStyle function
SetStyle();
// If not using this, it will throw the ArgumentError: Getting control ... when Repaint, it is required when adding more to the GUILayout on runtime
if (Event.current.type == EventType.Layout)
{
// If the player is not on chat
if (!CheckPlayer.isOnChat)
{
// Display the window
windowRect = GUILayout.Window(2, windowRect, ConsoleWindow, string.Empty, style);
}
// If acceptedChiefQuest boolean is true
if (acceptedChiefQuest)
{
// Remove the Previous Quest
RemovingQuest("Talk to the chief");
// Adding the New Quest
AddingQuest("Accepted the quest from the chief");
}
}
}
private void ConsoleWindow(int id)
{
// Position of the vertical scroll bar
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
// Loop through the List of the string
for (int i = 0; i < someTexts.Count; i++)
{
// Set label followed by the text of the string List and set the style
GUILayout.Label("Quest Description: " + someTexts[i], style);
}
// End of the vertical scroll bar
GUILayout.EndScrollView();
}
private void AddingQuest(string message)
{
// Add message to the string List from the string message parameter
someTexts.Add(message);
// Play the audio one time
audio.PlayOneShot(audioSEClip);
}
private void RemovingQuest(string message)
{
// Remove message from the string List from the string message parameter
someTexts.Remove(message);
}
}
And here is the script when the box is triggered by the player:
using UnityEngine;
using System.Collections;
public class Dialog : MonoBehaviour
{
private void OnTriggerEnter(Collider obj)
{
if (obj.gameObject.tag == "Portal Chief")
{
QuestManager.acceptedChiefQuest = true;
}
}
}
Here is the image when the program start (There is only one label):
But once the acceptedChiefQuest boolean become true, the label of `Accepted the quest from the chief" is adding more and more every single frame:
How can I fix this?
Your answer much appreciated!
Thank you very much!