Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Aphemra · Jan 26, 2016 at 05:26 PM · c#scriptingbasics

C# Beginner Question -- How to call a function from a separate script in multiple other scripts

This may be an obvious problem that is just not clicking to me and I've been googling for the last hour or two and has of yet, not been able to find a solution to my problem.

To start off, I'm creating an idle game and within this game, numbers can become quite large so I created a script that runs and input through a series of if-statements to determine whether it needs to be abbreviated and/or what abbreviation it needs dependent on how long the number itself is.

This script works find when testing from within the script itself. However, when I try to call it from another script in order to make different numbers auto-abbreviate, I continuously get a null exception stating the object reference is not set to an instance of the object. Being new to Unity and having not programmed in quite some time, I have absolutely no idea why this happens.

The following code is the script that converts the numbers.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class LargeNumberConverter : MonoBehaviour {
 
     string output = "";
 
     public string Convert(double number) {
         if (number >= 1e+123) {
             number /= 1e+123;
             output = number.ToString ("###.000 'dd'");
             return output;
         } else if (number >= 1e+120) {
             number /= 1e+120;
             output = number.ToString ("###.000 'cc'");
             return output;
         // (...omitted for length...)
         } else
             return output;
     }
 }

The following script is one example of trying and failing to call the script into another script.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class Click : MonoBehaviour {
 
     public Text clickPower;
     public Text followerDisplay;
     public float followers = 0.00f;
     public int followersPerClick = 0;
     public LargeNumberConverter lnc;
 
     void Update() {
         followerDisplay.text = "Followers: " + lnc.Convert((double)followers);
         clickPower.text = "Idol Power: " + lnc.Convert((double)followersPerClick);
     }
 
     public void Clicked() {
         followers += followersPerClick;
     }
 }

I would appreciate the help of anyone willing as I'd like to get passed this as soon as possible.

Comment
Add comment · Show 2
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 Taxen0 · Jan 26, 2016 at 05:43 PM 1
Share

Im typing this from my phone so im sorry if it gets wierd.

You need to instansiate your converter to an actual object. So try: public LargeNumberConverter lnc = new LargeNumberConverter();

Or make the converter a static class so you can call the methods that way ins$$anonymous$$d, i would probably do this but both works just as well.

avatar image hexagonius · Jan 26, 2016 at 05:53 PM 0
Share

do both scripts exist from the beginning? if so, assign the to the others field in the inspector.

2 Replies

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

Answer by flashframe · Jan 26, 2016 at 05:47 PM

With these kinds of helper methods, you don't really need to create an instance of your class to use them. You could make the Convert method static

 public static string Convert(double number) {
 }

and then you'd be able to call it from any script.

 followerDisplay.text = "Followers: " + LargeNumberConverter.Convert((double)followers);

Otherwise, if you want to do it your original way, you need to actually create an instance of your class first

 LargeNumberConverter lnc = new LargeNumberConverter();
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 Aphemra · Jan 26, 2016 at 06:00 PM

@Taxen0 and @flashframe

Thanks a ton! I totally forgot about static being a thing. Geez, it's been a long time since I opened up an IDE.

Glad to be back in it though, and thanks again!

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

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

Related Questions

When to use a namespace? 2 Answers

Am I using this Coroutine and IEnumerator correctly? 1 Answer

Could someone translate these to c#? 1 Answer

Unity Networking 1 Answer

How to make one Character follow behind another character? 0 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