- Home /
Is all scripting GameObject based?
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.
Answer by Keryu · Dec 07, 2011 at 06:50 PM
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#.
There are a wealth of functions you get access to by inheriting from MonoBehaviour
, but these are really only necessary if you want that script attached to a GameObject. http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.html
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:
1) MyClass.cs - which contains the MyClass
class and is not attached to a GameObject. 2) MyClassOnObject.cs - which contains the MyClassOnObject
class and must be attached to a GameObject to run.
I created an account to say this was very useful. Thanks !
just started using unity recently, this tip just shaved a LOT of time from my program, thanks :)
Note only classes derived from $$anonymous$$onobehaviour need to be in their own file with the class name matching the file name. For classes not derived from $$anonymous$$onobehaviour, you the names do not have to match, and you could put several classes in the same file. With classes derived from $$anonymous$$onobehaviour, you don't use a constructor. You do initialization in Awake() and Start(). For classes that are not derived from $$anonymous$$onobehaviour, you can use a constructor.
@davvilla - when you find something that is useful, click the thumbs up. This gives $$anonymous$$arma to the person who is answering the question as well as acknowledges it as being useful.
Super useful. It would be really helpful if Unity could make these points clear in their scripting tutorials. Thanks.
Thank you so much! I've been looking for exactly this kind of information for over a week. I've been program$$anonymous$$g in C# for years, and it seemed like Unity was entirely GameObject based, yet they have a tutorial on interfaces with only the code, and not how the code sits in Unity...caused confusion :).
Your answer
![](https://koobas.hobune.stream/wayback/20220613061855im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Not every Class in the API is derived from the "Object"-Class, so what are they? 2 Answers
Script to teleport player (Beginner here) 0 Answers
How to learn scripting without Unity 3 Answers
How to smooth between values? 2 Answers