Okay, I’m having a tough time finding this answer out…
As a rough overview of my question, my question is “is everything GameObject based?”
For background, I’m coming from many years of extensive Director programming, with some Flash. Generally, my concept of how programming goes is that somewhere there’s an entry point in the software where I can initialize a bunch of data and variables, and that software would go off and create graphic objects, and/or call functions to their behaviors.
For example, let’s say I’m doing a simple “find words in a grid” game. At the start of the program, I would go and load in my word list from an external data file into an array, and then I would initialize the letter tiles, randomly assigning each on a different letter graphic. Then I would activate a game state where the player could then start clicking on the tiles.
After running through a series of tutorials, and some amount of hours searching, I can’t find any reference to being able to create a bunch of variables on startup that aren’t attached to a GameObject in some shape or form.
Have I missed something? I’ve played around with ScriptableObjects a little bit…but then again, I’ve had to call and create those upon function Start of my camera, effectively linking that object to the camera.
Unity is largely a component-based environment where you create a script and attach it to an object in the world. However, you can run scripts without attaching them to GameObjects.
Any class that inherits from the MonoBehaviour class must be attached to a script. If you are using Javascript this is automatic, but you can choose to inherit from this class in C#. If you’re looking to take advantage of this, use C#.
You can create a class that does not inherit from MonoBehavior and has static properties that are accessed, like this:
//I do not inherit from MonoBehaviour, and therefor am not attached to a GameObject
public class MyClass
{
public static string myString;
}
//I inherit from MonoBehaviour, and therefor must be attached to a GameObject
public class MyClassOnObject : MonoBehaviour
{
string myName;
void Start()
{
myName = MyClass.myString;
}
}
Or, you can create an instance of a class and reference it that way:
//I do not inherit from MonoBehaviour, and therefor am not attached to a GameObject
public class MyClass
{
public string name;
public int age;
}
//I inherit from MonoBehaviour, and therefor must be attached to a GameObject
public class MyClassOnObject : MonoBehaviour
{
string myName;
int myAge;
void Start()
{
MyClass myClass = new MyClass();
myName = myClass.name;
myAge = myClass.age;
}
}
Edit: For clarification, please keep in mind these classes would need to be in their own script files! So, the MyClass class would have its own script and the ‘MyClassOnObject’ would have its own script. In C#, the name of the file must be the same as the class, so your project would have two files:
MyClass.cs - which contains the MyClass class and is not attached to a GameObject.
MyClassOnObject.cs - which contains the MyClassOnObject class and must be attached to a GameObject to run.