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
0
Question by dodi · May 29, 2014 at 10:32 PM · beginnercomponent

Scripts that aren't in an object

Hi everyone, I just started learning Unity yesterday so I'm not familiar on some of the concepts.

I understand that a script can be attached on an object and activated through the events or timer. I'm trying to make a way to validate the contents of the scripts in object A and object B.

My question is, does a script need to be attached on an object in order for it to run or can it be called by other scripts and how?

Here are some details of what I'm trying to make if it's necessary: I'm trying to make a turn based tactics game. I want to check for character details (which character is selected, is he eligible to move/attack, his current location, etc) and which action was selected after the character was selected (attack, move, skill). I'm currently using C# since it is what I'm familiar with compared to JS and Boo.

Thanks in advance

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 rutter · May 29, 2014 at 08:58 PM 0
Share

You can write static classes and functions that will be available anywhere. For example, many functions in the $$anonymous$$athf class are static; they don't belong to an object, and are just globally available.

You can also create global "singleton" objects, or create objects that aren't directly attached to a GameObject but are referenced by your other behavior scripts.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Clet_ · May 29, 2014 at 11:33 PM

You can have static classes. Those are classes that are not instantiated and are accessible throughout all the project, without any need to reference them. i.e.

 public static MyStaticClass {
 
     public static void HelloWorld () {
         Debug.Log("Hello WOrld!");
     }
 }
 
 public class MyMonoBehaviourClass : MonoBehaviour {
 
     private void Awake () {
         MyStaticClass.HelloWorld ();
     }
 }

You can also use normal classes (i.e. one that does not inherits from MonoBehaviour) and use this class' constructor to add a reference to them in other classes.

 public class MyNormalClass {
     
     public void HelloWorld () {
         Debug.Log("Hello World!");
     }  
 }
 
 public class MyMonoBehaviourClass : MonoBehaviour {
 
     private MyNormalClass a;
 
     private void Awake () {
         a = new MyNormalClass();
         a.HelloWorld ();
     }
 }
Comment
Add comment · Show 1 · 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 dodi · May 30, 2014 at 11:21 AM 0
Share

Thanks. The examples helped me a lot

avatar image
1

Answer by MikeNewall · May 29, 2014 at 11:33 PM

My question is, does a script need to be attached on an object in order for it to run or can it be called by other scripts and how?

If your script derives from MonoBehaviour then it must be attached to a game object. The reason you derive from MonoBehaviour in most cases is because it contains methods and properties for modifiying a game object and its components.

Non-Static

There are a few ways to write scripts which don't need to be attached to a game object. If you create an object which doesn't derive from MonoBehaviour then it can be instantiated with the new keyword like so:

 MeaningCalculator calc = new MeaningCalculator();

MeaningCalculator would contain methods and fields which you access like so:

 calc.CalculateMeaningOf("life");

This is useful when you require multiple instances of the same object. Say you want 3 different calculators you just instantiate 3 new calculators, and they exist independently of one another. However it will contain none of unity's functionality. You'd commonly use them for storing helper methods, data, and such.

Static

In cases where you need only one of something you can create a static class. A static class is the same as a non-static class except it can't be instanced. There is only one! This means you can access its members globally from any script.

Scriptable Object

Unity has an object called a Scriptable object which you can derive from to create objects which need not be attached to game objects. They're useful for storing data, and can also be saved as assets.

http://docs.unity3d.com/ScriptReference/ScriptableObject.html

Comment
Add comment · Show 1 · 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 dodi · May 30, 2014 at 11:24 AM 0
Share

Thanks. Even though the non-static example are somewhat unrelated to my main problem, they helped me fix a bug I ran into earlier.

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Adding a script component through scripting 1 Answer

Setting enabled on my objects children doesn't work 0 Answers

Do i need to learn C n C++ for unity scripting? 3 Answers

Beginner: For Loop freezing unity 1 Answer


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