I was thinking about a modular approach to better organize some data, but I am running into an issue:
Is there a way to access data between instanced sibling classes within a MonoBehaviour?
I only need one instance of each class, but if I make the classes static, it won’t serialize in the editor.
If I want to inherit from the MonoBehaviour, I am getting a stack overflow. And even if it worked, I don’t want “someInt” within “Input”.
It looks something like this (not the actual code, just to show the idea):
public class DoStuff : MonoBehaviour {
public int someInt = 9999;
[System.Serializable]
public class UI {
public int y = 0;
public void MoveUp (int i) {
i++;
}
}
[System.Serializable]
public class Input : DoStuff{
ui.MoveUp(ui.y);
}
public UI ui = new UI();
public Input input = new Input();
}
Any ideas? It’s not completely “necessary”, but it would be handy to collapse some stuff within the Editor without writing custom editors for all the things.
I do, but I’m on my phone right now and I wasn’t able to select the text to paste it into the code window right from the original post UI. It worked right after posting when editing. Not sure why, though.
EDIT:
Also, when trying to edit a post, I am getting one symbol per line until I rotate my phone. But I guess that’s off topic.
Also, after attempting to format, the code you showed is malformed. I cleaned it up some and added the necessary bits to make it not malformed.
public class DoStuff : MonoBehaviour {
public int someInt = 9999;
[System.Serializable]
public class UI {
public int y = 0;
public void MoveUp (int i) {
i++;
}
}
[System.Serializable]
public class Input {
void DoStuff {
ui.MoveUp(ui.y);
}
}
public UI ui = new UI();
public Input input = new Input();
}
Now looking at this you want to access ‘ui’ from ‘input’.
Thing is you need references to these instances. Where are those references? In class “DoStuff”.
So pass in a ref to DoStuff to each of the ui and input. There’s multiple ways of doing that… here is a simple one:
public class DoStuff : MonoBehaviour {
public int someInt = 9999;
[System.Serializable]
public class UI {
public int y = 0;
public void MoveUp (int i) {
i++;
}
}
[System.Serializable]
public class Input {
internal DoStuff owner;
void DoStuff {
owner.ui.MoveUp(ui.y);
}
}
public UI ui = new UI();
public Input input = new Input();
void Awake()
{
input.owner = this;
}
}
Note that just because the classes are nested in the same DoStuff class does not mean they’re “siblings” in any way. They don’t actually know about each other… and especially don’t know about the instance of the DoStuff class. What if multiple DoStuff’s exist? Which DoStuff is it accessing?
@lordofduct
Thanks for the detailed reply! I figured I’d have to somehow get the reference, but I sure had no clue that it would work like that! That’s great! I thought I couldn’t breach the gap by assigning it while running the code; I thought the reference needed to be there at compile time.
Also, sorry about wrong nomenclature! I have no formal education in this department.
Alright, just a small update: Can’t say my original idea works out nicely, it somewhat backfired, because as soon as I wanted to use coroutines, the nested classes would need to have inherited from MonoBehaviour, which, on the other hand, makes the fields disappear. So I’m just going for instanced nested classes as variable containers without any methods stored within. Would have been nice to shorten some method names, but, oh well.