- Home /
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;
}
}
}
}
}
}
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..
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.
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).
Your answer
Follow this Question
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