- Home /
Unity problems with classes C#
I have started learning Unity and already have learned JavaScript for web development so I do have some programming experience.
While programming in unity I came across a few things involving classes that I didn't quite get.
1) When I add code as a component of a unity object I write it inside the public class shown below. (name Mover is just an example.) However I never create an instance of this class so how does this work? All I see is the class being created.
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour {
}
2) Also shown in the code above is MonoBehaviour. I read the api and it said it is a base class. I never came across this in JavaScript. What does this mean and what does it do to the class Mover? I tried to look up what a base class was online and got a bunch of answers I didn't fully get. Here is what I understood from them. A base class is a class that doesn't inherit any properties, and other classes will inherit some properties from it. Is that right?
Answer by tobyfee · Mar 16, 2014 at 06:45 AM
1) I think I can handle: you'll be instantiating this class with the GameObject that you'll be adding it to as a component. So when you add it to your GameObject called 'Bug' and then drag your bug into the game, when the game starts it will instantiate Bug.Mover
2) All the classes you write (at first) will be applied to game objects within Unity, will be have their Start() methods called at start, their Update() methods called once per frame, etc. These behaviors are not part of basic javascript, but rather things especially implemented within Unity.
To make sure that all your classes have the same basic behaviors, they'll all extend the Monobehavior base class.
If you wanted to create a class that just had some delegates or parameters but didn't need to do any of the normal Unity stuff like being called on Update or referencing an attached game object, then you would create a class that doesn't extend Monobehavior.
Your answer
![](https://koobas.hobune.stream/wayback/20220613140347im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using AddComponent to add a Sub Class using a String 4 Answers
Will using a Monobehaviour script versus a non-Monobehaviour script use less memory? 2 Answers
How to store data in script and attach it later? 0 Answers
Question about classes on C# 1 Answer
How to structure code for squad selection and movement 0 Answers