Hello everyone!
So after numerous attempts to try to ignore unity as a game engine I finally settled on it. My reason for ignoring Unity is/was because I can’t seem to figure out how the code in unity works. I’m a pretty good C# programmer and this is giving me a headache with something that should be so easy.
So, can someone please show the way through my ignorance and help me out?
The problem is I have an initialization GameObject which runs some code and sets some variables up for the beginning of the game. I also have a DialogController script attached to the disabled UI canvas with the following code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class DialogController:MonoBehaviour {
public Text DialogText;
public Canvas DialogBox;
public void PrintText(string TextToPrint) {
DialogBox.GetComponent<Canvas>();
DialogBox.enabled=true;
DialogText=GameObject.Find("DialogText").GetComponent<Text>();
DialogText.text=TextToPrint;
Debug.Log("Test");
}
}
When I call DialogController.PrintText(“Some Text”) from the initalization script; nothing happens. What gives?
This is the code for the initialize.cs
[LIST=1]
[*]using UnityEngine;
[*]using System.Collections;
[*]
[*]public class Initialize:MonoBehaviour {
[*]
[*] // Use this for initialization
[*] void Start() {
[*] DialogController dc = new DialogController();
[*] dc.PrintText("test");
[*] Debug.Log("Test init");
[*] }
[*]}
[*]
The code looks fine (apart from your strange use of GetComponent and GameObject.Find). Try putting a Debug.Log after line 12 to see if it’s getting called at all.
Since you’re new to Unity and aren’t confident in your scripting skills in the engine, why not do some tutorials? https://unity3d.com/learn/tutorials
Debug.Log does nothing in terms of printing to my console. I also added it in the initialize.cs in the Start() method which also prints nothing to the console. I made a new initialize.cs file and renamed the old one to check if it was just not updating the code and it still does not work also with me adding only the call to the dialog. This is the code for the initalize.cs
using UnityEngine;
using System.Collections;
public class Initialize:MonoBehaviour {
// Use this for initialization
void Start() {
DialogController dc = new DialogController();
dc.PrintText("test");
Debug.Log("Test init");
}
}
I don’t know how to do/check that so I don’t think that would be it :s
Why does Unity hate me so much lol. Every time I go back to Unity I always get a problem with something extremely simple even from copying and pasting code and it never works. And yet, Unity is the one of the most used game engines. I don’t know how you people manage to use it without issues.
When getting started with something, it’s always frustrating. A few weeks ago I downloaded Unreal and I don’t think I’ll ever learn it in the little time I have. To me, the blueprints and c++ mixed together are much more challenging than Unity’s C#. It’s just a matter of experience and practice.
And of course, there are issues. I just got confirmation that a bug I filed with the rotate tool changing the global axis arrow direction was indeed a bug. However, this is C# coding and your code looks OK.
NullReferenceException: Object reference not set to an instance of an object
DialogController.PrintText (System.String TextToPrint) (at Assets/BinBash Studios/Scripts/UI/DialogController.cs:9)
Initialize.Start () (at Assets/BinBash Studios/Scripts/Initialize.cs:9)
The line it’s referring to is the method PrintText(string TextToPrint). I tried making it an object and converting the object to string but that does not work either. Also, it’s not instantiating anything so I have no idea what the console is talking about.
using UnityEngine.UI;
//in the Inspector, drag the Canvas onto this name
public Canvas DialogBox;
There’s no need to try to get a canvas on something you already specified is a canvas. Since we’re in a tough spot, you could even put Debug.Log(“Something happened – specify what is something”) after each line to see what isn’t completing.
There’s something wrong on line 9 of each script according to the console, but fixing one issue can fix multiple errors.
Do you mean that I need to remove the Public Canvas and the UnityEngine.UI? I don’t think I can because Text I believe is in UnityEngine.UI
That is what my inspector for the canvas looks like.
I also added Debug.Log after every line in Initialize.cs and DialogManager.cs and nothing is showing up in the console with debug messages being enabled.
using UnityEngine.UI; //this is required for UI
public Canvas DialogBox; //this is the object
void YourFunction()
{
DialogBox.SetActive(true);
}
Try running this script:
using UnityEngine;
using System.Collections;
public class TESTScript : MonoBehaviour {
void Start()
{
Debug.Log("You MUST see this in the console after hitting the play button");
}
Do you see anything in the Console? If nothing at all is showing up then it isn’t a scripting issue at all.
That works now even with the previous code entered in the initialize.cs. However, I still cannot use anything related to the DialogBox as the SetActive() method is not found. However you/we’ve made progress
This error is also the error mentioned in one of the above posts above.
DialogueBoxAppear is called by Initialize and the boolean is set to true.
I now have the issue above and the DialogueBox still does not appear.
I renamed the MakeDialogueBoxAppear() to ShowDialogueBox() to make the code a little neater.
Also, thank you so much for the help so far.
It is called in Initialize.cs which is attached to a gameobject and is called in the Start() function of said gameobject.
SetActive() doesn’t work to the reason being I don’t think its applicable right now.
I just saw your posts. I wasn’t ignoring you on purpose
I’m not offended by any of this, although I will say, I’d never want Canvas to be disabled, gameobject or component, simply because Canvas would be where both kinds of UI, on-screen information and Menus, would exist in. If I wanted to control the visibility of a specific layer of UI, I’d create empty gameObjects with RectTransforms in replacement of Transforms, under Canvas acting as UI layers, and turn those on or off.
using UnityEngine;
using System.Collections;
public class Initialize:MonoBehaviour {
// Use this for initialization
void Start() {
DialogController dc = new DialogController();
dc.ShowDialogBox(true);
Debug.Log("Test init");
}
}
DialogueController.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class DialogController:MonoBehaviour {
public Canvas DialogBox; //this is the object
public void ShowDialogBox(bool WillAppear) {
if(WillAppear==true) {
DialogBox.enabled=true;
}else { DialogBox.enabled=false; }
}
public void PrintText(string TextToPrint) {
//todo
}
}