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
1
Question by Morhaq · Dec 02, 2015 at 10:08 PM · scripting problemuirandom.range

Cannot read a random integer from another script.

Hey, I've been fiddling with Unity for a while know, learning to code myself through a lot of tutorials and pasting/re-writing/modyfing them to my needs. I decided its time to make something small but without help of any "outside" code, just from the docs and mind. And it made me stuck on this particular problem, i cannot read random generated integers from another script, they keep popping as 0 (zero) all the time, if you could tell me where I made my mistake I would be greatful, as for now, i've been searching for the answear on the forums+google but nothings seems to fix it, its eaither compilation error or the same outcome as it is now, I even re-wrote this thing using different methods. Here is what i've got:

The first random number generator, that also pops the number into UI text box

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class RandomNumberA : MonoBehaviour {
 
     public int a;
     Text numA;
 
     void Awake ()
     {
         numA = GetComponent <Text> ();
 
 
     }
     void Start () {
     
         int a = Random.Range(1,255);
         numA.text = "" +a;
     }
     
     void Update () {
     
     
     }
 }

There is another one that looks the same, but with int B, but then there is one script that should add both and make it a C

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 //using RandomNumberA;
 //using RandomNumberB;
 
 public class Add : MonoBehaviour {
     
     private int anum;
     private int bnum; 
     public int c;
     Text addnum;
 
     void Awake ()
     {
         addnum = GetComponent <Text>();
     }
     // Use this for initialization
     void Start () 
     {
         anum = GameObject.Find("VarA").GetComponent<RandomNumberA>().a;
         print ("A" +anum);
         bnum = GameObject.Find("VarB").GetComponent<RandomNumberB>().b;
         print ("B" +bnum);
         c = anum + bnum;
         print(c);
 
     }
 
     // Update is called once per frame
     void Update () 
     {
 }

}

int a is on game object in canvas called VarA the b is on the same Canvas called VarB

Console pops me with this:

A0 UnityEngine.MonoBehaviour:print(Object) Add:Start() (at Assets/Add.cs:22)

B0 UnityEngine.MonoBehaviour:print(Object) Add:Start() (at Assets/Add.cs:24)

0 UnityEngine.MonoBehaviour:print(Object) Add:Start() (at Assets/Add.cs:26)

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

1 Reply

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

Answer by tpusch · Dec 02, 2015 at 10:26 PM

The problem is that you are re-declaring the variable a, and I assume b in your Start() method. When you type int a = Random.Range(1,255) again it creates a new variable a that is local only to the start method and does not change your class member a. So instead just assign a random number to the a that you already created.

      public int a;
  
      void Start () {
      
          a = Random.Range(1,255);
          numA.text = "" +a;
      }
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 Morhaq · Dec 02, 2015 at 10:32 PM 0
Share

I did what you suggested, its still the same, nothing changed.

avatar image tpusch Morhaq · Dec 02, 2015 at 10:38 PM 1
Share

Ok, then the next step I would suggest would be assigning the value of your random a and b in awake ins$$anonymous$$d of start, since your Add script may be running its start before the RandomNumberA script.
Here is the manual for the order of unity functions: http://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html for extra info.

avatar image Morhaq tpusch · Dec 02, 2015 at 10:42 PM 0
Share

Yup, thanks, this worked!

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

44 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

Related Questions

i try make enemy health slider or any uI just show the health of Enemy Over head 0 Answers

UI Dropdown problem. 1 Answer

NullReferenceException on Text element 1 Answer

continously spanws and overlaps 0 Answers

Highscore - BestTime 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