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 Siccity · Jul 27, 2014 at 01:06 PM · optimizationfunctionstaticglobal

Adding static functions

Alright I guess this is more of a general programming question, but here goes; I want to make my function / script always active without it being added to the scene. I have a few different functions I use in many different scripts. Adding the script to the scene and pointing to it from the other scripts is a troublesome process. Is it a good practice to make such function static? Is there another way out?

This is one of the functions I use a lot

 //Gets the vector3 from a string
 public Vector3 getVector3(string rString){
     string[] temp = rString.Substring(1,rString.Length-2).Split(',');
     float x = float.Parse(temp[0]);
     float y = float.Parse(temp[1]);
     float z = float.Parse(temp[2]);
     Vector3 rValue = new Vector3(x,y,z);
     return rValue;
 }

This is the script I've made with the function(s)

 using UnityEngine;
 using System.Collections;
 
 public static class MyFunctions {
 
     public static Vector3 getVector3(string rString){
         string[] temp = rString.Substring(1,rString.Length-2).Split(',');
         float x = float.Parse(temp[0]);
         float y = float.Parse(temp[1]);
         float z = float.Parse(temp[2]);
         Vector3 rValue = new Vector3(x,y,z);
         return rValue;
     }
 }
 

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
0
Best Answer

Answer by MaT227 · Jul 27, 2014 at 01:12 PM

You are right this is more like a general programming question. Of course you can create static class with static value and static functions or a classic class but with static method and classic method.

It's up to you, it could be useful if you work in a team and create static functions accessible for other developers. It's also useful because using gameobjects in the scene consumes resources and affect performances and if you can use a static class with static functions why not doing it :) it will save ressources.

A great example is the Mathf class in Unity, there's no advantage of create a classic class, just do a static class and here we go.

Here is an interesting question asked on StackOverflow : Performance of static methods vs instance methods

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

Answer by smallbit · Jul 27, 2014 at 01:15 PM

Another option would be the singleton pattern.

http://unitypatterns.com/singletons/

Also if you don't want to to place your script in the scene at all, you can define a class that is not inheriting from MonoDevelop.

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 MaT227 · Jul 27, 2014 at 01:19 PM 0
Share

Of course, it depends of the usage. It's more related to pattern program$$anonymous$$g. A Singleton Pattern is adapted to manager classes for example.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Static function and variables error 2 Answers

Accessing non static members in a static function argument?? 1 Answer

Custom Global Functions 1 Answer

C# script for calculating the distance between the player and objects 1 Answer

Simple static function to display text onscreen (Accessible from anywhere) 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