Hai guys, 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. But I got this following error: ArgumentException: Getting control 0’s position in a group with only 0 controls when doing Repaint.
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;
private List<string> someTexts = new List<string>();
private Vector2 scrollPosition = Vector2.zero;
private Rect windowRect = new Rect();
private GUIStyle style = null;
public AudioClip audioSEClip = null;
private void Start()
{
windowRect = GameManager.WindowScreen(Screen.width - 350, 365);
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()
{
SetStyle();
if (!CheckPlayer.isOnChat)
{
windowRect = GUILayout.Window(2, windowRect, ConsoleWindow, string.Empty, style);
}
if (acceptedChiefQuest)
{
RemovingQuest("Talk to the chief");
AddingQuest("Accepted the quest from the chief"); // Argument Error in this line
}
}
private void ConsoleWindow(int id)
{
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
for (int i = 0; i < someTexts.Count; i++)
{
GUILayout.Label("Quest Description: " + someTexts[i], style);
}
GUILayout.EndScrollView();
}
private void AddingQuest(string message)
{
someTexts.Add(message);
audio.PlayOneShot(audioSEClip);
}
private void RemovingQuest(string message)
{
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;
}
}
}
Hi sir, I already did this and no error shown, but it adding the quest every single frame. Why is it like that sir? Could you please help me? Thank you.
Here is the code (The rest still same like the code above, only on OnGUI function):
private void OnGUI()
{
if (Event.current.type == EventType.Layout)
{
if (!CheckPlayer.isOnChat)
{
windowRect = GUILayout.Window(2, windowRect, ConsoleWindow, string.Empty, style);
}
if (acceptedChiefQuest)
{
RemovingQuest("Talk to the chief");
AddingQuest("Accepted the quest from the chief"); // Is adding every frame
}
}
}
The problem is, once you set acceptedChiefQuest to true, the method will continue running into that branch, that means you actually try to remove “Talk to the Chief” quest permanentely and add “Accepted the quest from the chief” continuously too.
Generally speaking, you have to disable the boolean variable as soon as you enter that branch for the first time. This way you make sure that it does not execute the code within the if statement the next time in runs (unless you set it to true externally).
Now, logically, you should also disable the possibility to enable acceptedChiefQuest again, e.g. deactivating the trigger, change its tag or anything else. Otherwise, you’ll do the same as soon as you re-enter that trigger and i assume that’s not what you want.
However, as you might have read in the post i linked above, it’s a good manner to avoid as much logic as possible in the OnGUI method for several reasons, mainly: it usually runs several times per frame. That is, it may run 3 times, 4 times etc until the next Update is executed etc.
That’s quite a lot of uneccessary overhead in most of the cases, as the game logic should generally be based on the usual Update routines.
The other reason is exactly what has been mentioned in the post i linked. It’s simply error prone, or let’s say it often produces results that you do not expect.