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 Barsonax · Sep 29, 2015 at 10:07 PM · arrayarraysreferencestatic

Cannot access a array in another script

I have 2 scripts and iam trying to acces the array thats made in script 1 but it gives me a nullreferenceexception when i press the start button (which calls Startgame()). Strangely when i use the debug.log in the first script it gives me the corrrect value 100 when i start the game. Game.current.Progress also doesnt have this problem it just works as i expected it to but this is not a array. Is this a bug? Kinda confused now. Do i have to reference a array in a different way than a normal variable?

Script 1:

     using UnityEngine;
         using System.Collections;
         
         [System.Serializable]
         public class Game { 
             
             public static Game current = new Game ();
             public int Progress;
             public float[] Scores;
         
                 
             public Game () {
                 Progress = new int();
                 Scores = new float[2];
                 Scores[0] = 100;
                 Debug.Log (Scores [0]);
             }
             
         }
 
 
 

Script 2 :

     using UnityEngine;
     using UnityEngine.UI;
     using System.Collections;
     
     public class Menucontroller : MonoBehaviour {
     
     
     
         void Start (){
     
             SaveLoad.Load ();
             foreach (Game g in SaveLoad.savedGames) {
                 Game.current = g;
             }
             if (Game.current.Progress == 0) {
                 Game.current.Progress = 1;
     
             }
         }
     
         public void StartGame () {
             Application.LoadLevel (2);
             Debug.Log ((Game.current.Scores)[0]);
     
     
 

            }
 
 Etcetcetc





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 bubzy · Sep 30, 2015 at 10:36 AM 0
Share

shouldnt Debug.Log ((Game.current.Scores)[0]);

be

Debug.Log (Game.current.Scores[0]);

2 Replies

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

Answer by Barsonax · Oct 10, 2015 at 10:57 PM

I solved it by making a public static variable that i use as a "interface" between my objects. Since i know there will be only 1 instance of this this seemed a quite simple solution to me:

public static ItemDatabase Interface;

Awake() { Interface = this; }

Then i could do somescript.Interface to gain a reference to a script.

I will look in your suggestion though as i have also read there are disadvantages about static variables.

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 landon912 · Sep 30, 2015 at 12:06 AM

You need to actually gain a reference to the class instance. You can't simply use a static class qualifier. You can "get" a reference through the other class like this if there is only one instance in the scene:

 Debug.Log(FindObjectOfType<Game>().Scores[0]);

However, it'd be better to cache the reference, and use a field.

Even better, you should use the singleton design pattern.

Comment
Add comment · Show 2 · 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 Barsonax · Sep 30, 2015 at 06:24 AM 0
Share

Tried this but since the first script doesnt use monobehavoir this doesnt work. Wouldnt really make sense to me either since i can reference the int in the other script just not the array.

avatar image landon912 Barsonax · Sep 30, 2015 at 10:19 AM 0
Share

Comment please, and edited my answer.

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

33 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

Related Questions

Why i can't read a Variable's Length(Array) 2 Answers

Adding a new GameObject to an Array without overriding the others 0 Answers

help ordering array for planetary gravity 1 Answer

Why are my arrays in arrays null (converting from JSON)? 1 Answer

Object Reference not set somewhere in the List, but can't figure it out. 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