- Home /
Trying to run 5 instances of a static method at once to do an action, probably a dumb thing to do?
I've been game programming off and on for many years, but I've only been learning C# for a month or so now...
So, so far what I've been attempting is using public static methods that are called from a main game class. It's worked so far, allowing me to trigger actions based on game frames. I've easily been able to make fade out, fade in, move objects around, through these static classes. Static classes can be called even without something being attached to that .cs file after all...
The idea with this was to put lists on objects that would be read off from an interpreter, and again, so far, the idea has been working just fine. Only the MainGame .cs file has to be attached to an object here:
using System.Collections;
using System.Collections.Generic;
public class MainGame : MonoBehaviour {
public static List<string> CurrentCommands = new List<string>;
void Start () {
}
void Update () {
MoveObject.Update (CurrentCommandArgs[0],CurrentCommandArgs[1] etc etc...);
}
}
public class MoveObject : MainGame {
public static int State = 0;
public static void Update() {
if (State == 0 && CurrentCommands[0] == "MoveObject") {
State = 1;
}
if (State == 1) {
//Run
State = 2; //End
}
}
}
}
Now I'm coming to a point in where I've played around with this enough and I've found the flaw...I can't run multiple instances of this example MoveObject class with this.
What I need is to instantiate classes as objects that run independently...
I've been trying to find a workaround...I need to be able to instantiate classes with the "new" keyword, but then that's not allowed using MonoBehaviour (and I know, for good reason). If worse comes to worse I think I'll just have to use Coroutines.
I'm looking for a way in where a class will run on it's own without attaching it to an object if that is at all possible, just using that single MainGame.cs script, but I'll probably have to surrender at this point. : / What do you think?
Unity tells me "use AddComponent instead of the new keyword/instantiating".
Your answer
Follow this Question
Related Questions
MonoBehaviour methods in non-MonoBehaviour class 1 Answer
Entering false statement even it is at true state. 1 Answer
Update() in non-component script? 3 Answers
General programming question on Static Classes 1 Answer
Using a static class & pass in gameObject or just add same script to multiple objects 1 Answer