I’m a noob on classes and scope, but I’m trying to understand it.
I have this simple class, but the value “debug1” never changes when calling the setter. “Setting XXX” is shown, but why isn’t it being set? The class and the SetDebug method are public. The value is private, but I thought it should be, since the SetDebug is changing it internally.
using UnityEngine;
public class DebugTest : MonoBehaviour {
private string debug1 = "x";
void Update() {
// This shows the original value - WHY?
print("debug1 = " + debug1);
}
public void SetDebug(string text) {
print("Setting " + text);
debug1 = text;
}
}
Set the value from another script:
DebugTest Name = new DebugTest();
Name.SetDebug("test");
Also is there any simpler way of calling the method in one line?
Thanks!
That’ll work fine but don’t create MonoBehaviour instances via construtor. Use AddComponent to put them on a GameObject
Hmm, I guess I shouldn’t inherit Monobehaviour, normally, but if I remove it I get ‘DebugTest’ is not a known identifier. How can I call it, and should the class or method be static, or how will I call it?
I’m actually using this inside an FPS-counter, that shows info on a OnGUI, so I guess Monobehaviour should be inherited. But why isn’t the values being set using
Any sample code to fix my issues is appreciated
Code looks fine. Like I said, I think you’re getting screwy results because you’re not making a MonoBehaviour the “right way”. Do this and see how it goes
var g = new GameObject("test");
var t = g.AddComponent<DebugTest>();
t.SetDebug("test");
I couldn’t get it working, wasn’t sure what “test” was in the part new GameObject(“test”); so I did something else. After a while I realized this was one of the most asked questions in Unity…
But how do I assign the script FPSCounter of the object “Main Camera” without dragging it in the inspector? I guess it’s something with the code above…
Here are a working example with my actual variables/methods:
// In the start of the current class (assign this i the inspector to the object containing the method I want to call)
public FPSCounter fpscounter;
// To call the method
fpscounter.SetDebug("text to send"));
If for whatever reason you don’t want to drag it, you could do this, I think:
// in the FPSCounter script
void Start() {
Camera.main.AddComponent<this>();
}
Interesting, but how does the FPSCounter script run it’s Start() method, if it’s not attached to anything?
However I was a bit unclear, I meant, instead of dragging the Main Camera to a serialized/public field in my player object, how can I get the same result using code to be able to access it? Maybe it’s what KelsoMRK wrote above?
Well, he used just a new game object. (wasn’t the camera). Then attached the script.
Sorry, perhaps I overlooked that the script wasn’t attached to anything lol.
Some script somewhere has to add it; whether it’s added to the camera or anything in the game, I doubt that matters unless you are needing that.
So, using the example of a new game object, as mentioned above could work.
Do you want to add the DebugTest or FPSCounter or both somewhere?
The idea is that I have an FPSCounter script (shows a GUI), attached to the Main Camera, which now is turning into a debug-script to show me debug info, instead of looking at the console. So I’m considering renaming it, since it won’t be in the final game. But for now it’s called FPSCounter.
It would be ideal to use one line in a script anywhere, to output something to the debug GUI. Currently I don’t have so many scripts, so it’s ok to attach the camera to a public var of the player, but it would be easier to just use one or two lines of code to output anything. Of course it’s also ok to add a line to Start() of each script that needs to print debug info.
I’m very new to Unity, so sorry if I’m asking weird questions, and the confusion of different class names, that I haven’t really decided on yet.
Okay, I think I understand most of what you’re saying.
You could try this:
public class FPSCounter : MonoBehaviour {
static FPSCounter instance;
[SerializeField]
Text outText; // I don't know how yours is setup. ;)
void Awake() {
instance = this;
}
public static void SetDebug(string msg) {
instance.outText.text = msg;
}
// in another class that wants to show/set Debug text..
FPSCounter.SetDebug("Whatever debug message you want");
I do not see any good reason why you can’t just drag this onto the camera for when you’re in the editor. Then just remove it later
So, this doesn’t deal with adding it, but it does show one way that you can access that script from anywhere without a variable. This is not the only way to get it working, but if you just want some help to be able to send debug messages to the UI, something like this should be good for you to work with for now
Hope it helps.