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 JerryCic · Jan 02, 2012 at 11:19 PM · c#globalvariables

How do i create a global structure and how do i acces it from any script?

My first feeble attempt is to create a script and attach it to the main camera. I managed to do it error free, but how do i access it to modify its content?

Here is my script:

 using UnityEngine;
 using System.Collections;

 public class Song : MonoBehaviour {
 // Home for the Main Data structure
 // Is an Array of Measures
 // Each Measure is an array of Chords
 // Each Chord is an array of Notes
 // Each Note is an Array of Game Objects
 public class Notes
 {
     public GameObject[] Sound;
 }
 public class Chords
 {
     public Notes[] Note;
 }
 public class Measures
 {
     public Chords[] Chord;
 }

 public int MaxMeasures;
 public int ChordsPerMeasure;
 public int NotesPerChord;
 public int MaxObjectsPerNote;

 Measures[] Measure;
 
 void Start()
 {
     Measures[] NewMeasures = new Measures[MaxMeasures];
     Measure = NewMeasures;
     int M = 0;
     for (M = 0; M < MaxMeasures; M++)
     {
         Measure[M] = new Measures();
         Measure[M].Chord = new Chords[ChordsPerMeasure];
         int C = 0;
         for (C = 0; C < ChordsPerMeasure; C++)
         {
             Measure[M].Chord[C] = new Chords();
             Measure[M].Chord[C].Note = new Notes[NotesPerChord];
             int N = 0;
             for (N = 0; N < NotesPerChord; N++)
             {
                 Measure[M].Chord[C].Note[N] = new Notes();
                 Measure[M].Chord[C].Note[N].Sound = new GameObject[MaxObjectsPerNote];
                 int S = 0;
                 for (S = 0; S < MaxObjectsPerNote; S++)
                 {
                     Measure[M].Chord[C].Note[N].Sound[S] = null;
                 }
             }
         }
     }
 }

}

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

3 Replies

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

Answer by Rod-Green · Jan 03, 2012 at 12:28 AM

You can implement a singleton pattern which will allow you public static access to methods and properties..

Some good info here: http://answers.unity3d.com/questions/20949/singleton-implementation.html

Or otherwise you can 'find' the monobehaviour in the scene with something like..

   Song[] songs = (Song[])FindSceneObjects(typeof(Song));

and then pass that list and handle / access as needed..

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 JerryCic · Jan 03, 2012 at 01:41 AM

@RodGreen

Thanks for your responce.

I read a lot of your link.

So i tried this. I simply made

 Measures[] Measure;

into

 public static Measures[] Measure;

I also re-attached the script to the camera object. It seems to be working. I attached it to the camera in order to defend against the issue of multi thread safty as oulined in your link. I think that this will instantiate the song object during the program load instead of at first call.

Thanks for steering me in the right direction.

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 tayl0r · Jan 03, 2012 at 02:19 AM

The other thing you can do is add a public member variable for your Song object into your other scripts, and then in the Inspector, drag your Main Camera from the scene into your script input for the Song member variable.

Example:

 public class Instrument : MonoBehaviour {
     public Song mySong;

If you drag the Camera over to fill in the "mySong" variable, then in your Instrument script you will have a reference to the Song in your Camera. You can do this because Camera is a Song (because you attached your Song script to your Camera).

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

null refference exception. right after i dimention the object array?? 2 Answers

animation.Play (MissingComponentException: There is no 'Animation' attached to.. 2 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