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 Wisearn · Apr 08, 2014 at 10:50 AM · c#staticprogramming-basics

Make my script accessable just by its namespace?

Preferrably I don't want to have to "find" the script or make an instance of it with a singleton work-around to use its functions and stuff, is there any way to make the script open and accessable from another script as if the function was right here, except by namespace?

like if I have mathutils.cs: class mathutils { void multiplication(int x, int y) {whatevz;} }

And then in another script called gamelogic.cs I'd like to be able to just write mathutils.multiplication(1, 2); anywhere in the gamelogic.cs.

Is this possible? mathutils needs to be singleton, but I'd like to be able to also create instances of subclasses inside mathutils if possible.

Thanks a bunch in advance!

Comment
Add comment
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

2 Replies

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

Answer by Hoeloe · Apr 08, 2014 at 11:08 AM

Try a `using` statement.

Also, if you want to use methods or variables without specific object references; that is, using something like:

 MyClass.Method();

Instead of:

 MyClass c = new MyClass();
 c.Method();

Have a look at static members.

On an unrelated note, it's good practice to use capital casing for your class names (e.g. MathUtils instead of mathutils) to separate them from variables, which usually use lower camel-casing (e.g. myVar instead of myvar or MyVar). It keeps your code neat and prevents confusion later.

Comment
Add comment · Show 3 · 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 Wisearn · Apr 08, 2014 at 11:28 AM 0
Share

$$anonymous$$aking it static has the effect I want, but the problem is "cannot declare instance members in a static class"?

Is there any way to get the same effect of making it static and being able to just write $$anonymous$$yClass.$$anonymous$$ethod(); while still being able to create instances of subclasses and such?

Also; if I use "$$anonymous$$yClass c = new $$anonymous$$yClass();" how do I make $$anonymous$$yClass singleton for this purpose?

avatar image Hoeloe · Apr 08, 2014 at 12:23 PM 0
Share

You need to make the method static, not the class. Please read the page about how static works again and make sure you fully understand it.

avatar image Wisearn · Apr 08, 2014 at 01:18 PM 0
Share

Oh yes cool thanks! I'll try it out in a bit.

avatar image
0

Answer by NachoUJI · Apr 08, 2014 at 11:59 AM

You can create the object, not being part of the scene:

 class Mathutils { 
 
   public void multiplication(int x, int y) 
   {
     retutn x*y; 
   }
 
 }

Then in the other script you can do:

     Mathutils objectMath = new Mathutils();
     objectMath.multiplication(myX, myY);

You don't need to write the ":MonoBehaviour" in the declaration of the class.

Hope it helps.

Comment
Add comment · 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

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

Will making all variable static reduces the game performance? 2 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

GameObjects static array NullReferenceException 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