Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by SlomianyScott · Dec 07, 2011 at 06:36 PM · scriptingbasics

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shongololo · Mar 26, 2014 at 05:52 PM 0
Share

You read my $$anonymous$$d...

1 Reply

· Add your reply
  • Sort: 
avatar image
11

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image davvilla · Mar 22, 2012 at 03:30 PM 0
Share

I created an account to say this was very useful. Thanks !

avatar image bubzy · Jun 01, 2013 at 02:10 AM 0
Share

just started using unity recently, this tip just shaved a LOT of time from my program, thanks :)

avatar image robertbu · Jun 01, 2013 at 02:35 AM 2
Share

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.

avatar image shongololo · Mar 26, 2014 at 05:52 PM 1
Share

Super useful. It would be really helpful if Unity could make these points clear in their scripting tutorials. Thanks.

avatar image Palimon · Aug 05, 2014 at 04:53 PM 0
Share

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 :).

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges