x


Global Variables with Prefabs/ Self-Naming Variables

Essentially, in my project there is a prefab, known as a "latch". The latch can be instantiated by players, leading to an unknown amount of latches. The problem is that each latch needs to have a separate global variable, or some way to communicate with the scene's other objects while distinguishing itself from the other prefabs.

Now, I have an idea, but I don't know the coding to create it - or whether it's possible. Could a new latch, upon entrance to the scene, "count" all the other latches then make a new global variable based upon how many other latches currently exist? (Example: If the latch is the 64th, then it would create a global variable "latch64".) Can this be done? If so, how? If not, does anybody else have any ideas?

more ▼

asked Aug 07, 2011 at 03:59 AM

Capt_Paradox gravatar image

Capt_Paradox
1 4 4 4

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

have a MasterScript.

static var latchList : List. = new List.(); Now when your new Latch is instantiated, have something like this

// create the new GameObject
newLatch = new GameObject();

// make this GameObject a "Latch"
newLatch.AddComponent(Latch);
var theLatch = newLatch.GetComponent(Latch);  

// Add the Latch to the Global Array
MasterScript.latchList.Add(newLatch);

// achieve corresponding numbering
newLatch.myNumber = MasterScript.Count;
//of course, Latch.js needs a var myNumber : int;
Hope that helps. Greetz, Ky.
more ▼

answered Aug 07, 2011 at 07:15 AM

SisterKy gravatar image

SisterKy
3.1k 81 75 100

(comments are locked)
10|3000 characters needed characters left

Thanks! Sorry, I know a bit of code, but I'm no master. But that should definitely work!

more ▼

answered Aug 07, 2011 at 06:00 PM

Capt_Paradox gravatar image

Capt_Paradox
1 4 4 4

np =) just next time... write your thanks as a comment ;)

Aug 07, 2011 at 08:22 PM SisterKy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1343
x164
x21

asked: Aug 07, 2011 at 03:59 AM

Seen: 1001 times

Last Updated: Aug 07, 2011 at 08:22 PM