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 imgregduh · Jun 19, 2013 at 10:23 PM · variablearrays

make a variable for an array from another script ?

i was curious if it was possible to make a variable for an array from another script. so i dont have to type something like this over and over again

 wordDBScript.Root[1];

and make it like

 wordTemp[1];
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 DannyB · Jun 21, 2013 at 10:43 AM

I am not sure if this works in Javascript, but in C#, yes you can. This is sometimes called square brackets operator overloading, or Indexers.

Basically, you need to define an overload function that handles all requests to YourClass[]

Attach this script to an empty GameObject to see it work:

 using UnityEngine;
 using System.Collections;
 
 public class GeneralTest : MonoBehaviour {
 
     public class ChildClass {
 
         string[] myStringArray = new string[] { "Hello", "World" };
 
         // This is the overload. Return a string whenever
         // MyChild[int] is called
         public string this[int index] {
             get { return myStringArray[index]; }
             set { myStringArray[index] = value; }
         }
     }
 
     void Start() {
         ChildClass child = new ChildClass();
         Debug.Log( child[0] + " " + child[1] );
         child[0] = "Tada";
         Debug.Log( child[0] + " " + child[1] );
     }
 }

More info:

  1. Indexers on MSDN (I hate that site, but its the official manual...)

  2. Indexers on DotNetPerls (the C# guide for the rest of us)

  3. A related answer on StackOverflow

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 imgregduh · Jun 21, 2013 at 01:47 PM 0
Share

im having some trouble applying the code above.

I actually got a stackoverflowexception. probably because im declaring it and the array from script B isnt handing it over. let me re-explain what i meant Script B as an array I want and Script A will present the array in the buttons on the GUI. The only way I got the buttons to show the words from Script B is to declare an instance and call the instance (wordDBscript.Root, root being the array variable).

im just curious if i can shorten "wordDBscript.Root[1]" to something like "wordtemp[1]" Script A: using UnityEngine; using System.Collections;

 public class LevelScript : $$anonymous$$onoBehaviour {
 
     float ButtonWidth = (Screen.width)/5;
     float ButtonHeight = Screen.height/50 +Screen.height/25;
     float ScreenToButtonWidth = Screen.width/25;
     float RowOneButtonPosY;
     float RowTwoButtonPosY;
     public string[] visibleRoots = new string [8];
     
     GameObject wordDB;
     WordDatabaseScript wordDBScript;
     
     // Use this for initialization
     void Start () 
     {        
         RowOneButtonPosY = (Screen.height*3)/4;
         RowTwoButtonPosY = (Screen.height*3)/4+ButtonHeight+Screen.height/30;
         //visibleRoots = GameObject.Find("_WordDatabase").GetComponent<WordDatabaseScript>().Root;
 
     }
     void Awake()
     {
         wordDB = GameObject.Find("_WordDatabase");
         wordDBScript = wordDB.GetComponent<WordDatabaseScript>();
         
         
         //System.Array.Copy(wordDBScript.Root,visibleRoots,8);        
     }
     
     // Update is called once per frame
     void Update () {
         print(ButtonWidth);
         print(ButtonHeight);
 
     }
     void OnGUI()
     {
         
         GUI.Button(new Rect(ScreenToButtonWidth,RowOneButtonPosY,ButtonWidth,ButtonHeight),wordDBScript.Root[0]);
         GUI.Button(new Rect((ScreenToButtonWidth*2)+ButtonWidth,RowOneButtonPosY,ButtonWidth,ButtonHeight),wordDBScript.Root[1]);
         GUI.Button(new Rect((ScreenToButtonWidth*3)+ButtonWidth*2,RowOneButtonPosY,ButtonWidth,ButtonHeight),wordDBScript.Root[2]);
         GUI.Button(new Rect((ScreenToButtonWidth*4)+ButtonWidth*3,RowOneButtonPosY,ButtonWidth,ButtonHeight),wordDBScript.Root[3]);
         GUI.Button(new Rect((ScreenToButtonWidth),RowTwoButtonPosY,ButtonWidth,ButtonHeight),wordDBScript.Root[4]);
         GUI.Button(new Rect((ScreenToButtonWidth*2)+ButtonWidth,RowTwoButtonPosY,ButtonWidth,ButtonHeight),wordDBScript.Root[5]);
         GUI.Button(new Rect((ScreenToButtonWidth*3)+ButtonWidth*2,RowTwoButtonPosY,ButtonWidth,ButtonHeight),wordDBScript.Root[6]);
         GUI.Button(new Rect((ScreenToButtonWidth*4)+ButtonWidth*3,RowTwoButtonPosY,ButtonWidth,ButtonHeight),wordDBScript.Root[7]);
     }
     void ButtonAssign()
     {
         
     }
 }


ScriptB:`using UnityEngine; using System.Collections;

public class WordDatabaseScript : $$anonymous$$onoBehaviour { public string [] Root;

 // Use this for initialization
 void Start () {

     
 }
 
 // Update is called once per frame
 void Update () 
 {
     BeginnerStage();
 }
 void BeginnerStage()
 {
     Root = new string[] {"one","two","three","four","five","six","seven","eight"};
 }
 

} `

avatar image DannyB · Jun 21, 2013 at 01:57 PM 0
Share

I am not sure where are you having difficulties... are you simply looking for a shorter way to write things in your script?

If you do:

 string[] wordtemp = wordDBScript.Root;

You will be able to use wordtemp[0]

avatar image imgregduh · Jun 21, 2013 at 02:01 PM 0
Share

funny story, yes and I thought of that line of code above this comment and it didnt work at first then i was like maybe i put it in the wrong spot just now and it works fine. Your bracket overloading idea may become of use to me later, thank you for your help

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

15 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

Related Questions

Updating variable on another script. It is not working. 2 Answers

accessing a variable from another script. 3 Answers

How to make two scripts share same variable? 1 Answer

Unity thinks my animation is not attached, but it is. 2 Answers

Script with variable number of variables ? 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