How assign gameobjects to a string and reference them again?

Hello UnityAnswers- I need help figuring and setting this up. Here is the situation- I would like to set, store/recall creatures with buttons that are automatically assigned to them when I allow them in my party.

I have no idea on how to set this properly, so I'll attempt something we can start on...I know it's lame, but it's all I understand to tackle this yet :(

On Beasts speech

Long story short--

  1. Creature asks to join your party..
  2. You say yes/no
  3. if you say yes:

    do something like ( `Hero.Beast *= gameObject;` ) ???

  4. * *
* *

On Hero's script

* *```* *static var Beast : GameObject [];* *function OnGUI(){* *var LineupInt : int = -1;* *var LineupStrings : String[] = ["Beast1", "Beast2", "Beast3"];* *LineupInt = GUI.Toolbar (Rect (25, 25, 250, 30), LineupInt, LineupStrings);* *if(LineupInt == 0) // press Button1* *if (Beast.length > 0){* *if(Beast[1] != null)* *Beast[1].SetActiveRecursively(false);* *else if(Beast[1] == null)* *Best[1].SetActiveRecursively(true);* *}* *if(LineupInt == 1) // press Button2* *if (Beast.length > 0){* *if(Beast[2] != null)* *Beast[2].SetActiveRecursively(false);* *else if(Beast[2] == null)* *Best[2].SetActiveRecursively(true);* *}* *etc* *```* *

So, how do I tell the Hero's script Beast[] string to assign someone to the next available number, then reference them again specifically with the buttons?

* *

Thanks for any help! ( ps- how bad is my attempt too? I'M TRYING! :)

* *

EDIT: My answer

* *

Okay, here is the set up:

* *

in the dialogue script, if you accept the offer to join your team, I said this

* *

Hero.Beast.Push (gameObject); to add to the array

* *

in the Hero's script I put this

* *```* *static var Beast = new Array();* *var beast1 = false;* *var beast2 = false;* *var beast3 = false;* *var beast4 = false;* *function OnGUI () {* *var LineupInt : int = -1;* *var LineupStrings : String[] = ["Beast1", "Beast2", "Beast3"];* *LineupInt = GUI.Toolbar (Rect (25, 25, 250, 30), LineupInt, LineupStrings);* *// Beast ONE* *if (LineupInt ==0)* *if(Beast[0] != null){* *print(Beast[0]);* _//** The first is set at the start of the level- will edit later **_ *// Beast TWO* *}if(LineupInt ==1)* *if(!beast2)* *if(Beast[1] != null) {* *print("Beast 2 is stored");* *LineupStrings[1].name = Beast[1].name; //(???) see comment* _Beast[1].transform.position = transform.position + transform.right * -4 + transform.up *7;_ *Beast[1].transform.parent = transform;* *Beast[1].SetActiveRecursively(false);* *LineupInt = -1;* *beast2 = true;* *}if(LineupInt ==1)* *if(beast2){* *print("Beast 2 is released");* *Beast[1].SetActiveRecursively(true);* *Beast[1].transform.parent = null;* *LineupInt = -1;* *beast2 = false;* *//And so on and so forth for each button in the toolbar* *```*

I'm not sure exactly what you're trying to do, but you can add elements to an array of arbitrary size by using Push().

    var activeBeastArray = new Array();

    for ( var currentBeast : Beast in someArrayOfBeasts ) {

       if ( heroWelcomesBeast(currentBeast) ) {
         activeBeastArray.Push(currentBeast);
       }
    }

print(activeBeastArray.length + " beasts have been welcomed out of " + someArrayOfBeasts.length);

Also, there's no need to have so many if statements in your code in this situation. Why not replace your code with this:

lineupInt = GUI.Toolbar(someRect, lineupInt, labelArray);

if ( Beast.length > 0 ) {

  if ( Beast[LineupInt] ) {
    Beast[LineupInt].SetActiveRecursively(false);
  }
}

If Beast[LineupInt] is null, you probably don't want to call a function on it like you suggest. Calling a member function of a null object doesn't usually go over very well (it will throw a null pointer exception).

What is the function SetActiveRecursively supposed to do? It's very difficult to determine what you want to accomplish based on your post. Is the list of Beasts static and there should always be one active Beast?

In that case you might have something like this:

var beastArray : Beast [];

function OnGUI ()
{
  var lineupInt = GUI.Toolbar(someRectangle, lineupInt, getBeastNames());

  setActiveBeast();    
}

/* 
 * iterate over array of beasts and set isActive to true if the current
 * beast was selected.  set isActive to false otherwise
 *
 * this will leave one active beast
 *
 */

function setActiveBeast ()
{
  for ( var i = 0; i < beastArray.length; i ++ ) {
    beastArray*.isActive = ( i == lineupInt );*
 *}*
*}*
*class Beast*
*{*
 *var name : String;*
 *var isSelected : boolean;*
*}*
*function getBeastNames ()*
*{*
 *var theNames = new Array();*
 *for ( var i = 0; i < beastArray.length; i ++ ) {*
 _theNames.Push(beastArray*.name);*_
 _*}*_
 _*return theNames;*_
_*}*_
_*```*_
_*<p>Anyways... Not sure if any of that is helpful.  Good luck.</p>*_
_*<hr>*_
_*<p>EDIT: after original poster's comments</p>*_
_*<hr>*_
_*<p>OK... How about something like this:</p>*_
_*```*_
_*var beastArray : Beast [];*_
_*var buttonStartX = 10;*_
_*var buttonStartY = 10;*_
_*var buttonWidth  = 100;*_
_*var buttonHeight = 20;*_
_*function OnGUI()*_
_*{*_
 <em>_/* print a button for each beast in beastArray */_</em>
 _*for ( var i = 0; i < beastArray.length; i ++ ) {*_
 <em>_/* if a beast's button gets clicked, he will go bezerk */_</em>
 <em>_if ( GUI.Button(getButtonRect(i), beastArray*.name) ) {*_</em>
 <em><em>_beastArray*.goBezerk();*_</em></em>
 <em><em>_*}*_</em></em>
 <em><em>_*}*_</em></em>
<em><em>_*}*_</em></em>
<em><em><em>_/* function to help arrange a list of buttons vertically */_</em></em></em>
<em><em>_*function getButtonRect ( theOffset )*_</em></em>
<em><em>_*{*_</em></em>
 <em><em><em>_var yPos = buttonStartY + theOffset * buttonHeight;_</em></em></em>
 <em><em>_*return Rect(buttonStartX, yPos, buttonWidth, buttonHeight);*_</em></em> 
<em><em>_*}*_</em></em>
<em><em><em>_/*_</em></em></em> 
 <em><em><em>_* Definition of Beast class_</em></em></em>
 <em><em><em>_*_</em></em></em>
 <em><em><em>_* In this code a Beast object has a Transform associated with it._</em></em></em>
 <em><em><em>_* This script will allow you to create an array of Beasts in the_</em></em></em>
 <em><em><em>_* Inspector.  You could drag a Transform (or some other object_</em></em></em>
 <em><em><em>_* type after changing the code) onto each Beast object._</em></em></em>
 <em><em><em>_*_</em></em></em>
 <em><em><em>_*/_</em></em></em>
<em><em>_*class Beast*_</em></em>
<em><em>_*{*_</em></em>
 <em><em>_*var name : String;*_</em></em>
 <em><em>_*var myTransform : Transform;*_</em></em>
 <em><em>_*function goBezerk ()*_</em></em>
 <em><em>_*{*_</em></em>
 <em><em>_*Debug.Log("My name is " + name = " and I am going bezerk!");*_</em></em>
 <em><em>_*}*_</em></em>
<em><em>_*}*_</em></em>
<em><em>_*```*_</em></em>
<em><em>_*<p>This code will iterate through an array of Beast objects.  It will draw a button for each Beast.  If the button is clicked, the corresponding Beast will do something (call one of its functions or have a function called on it).  How you populate the array of Beasts is another question.  If you use this code, you could at least drag and drop Beast objects into the Inspector for testing purposes.</p>*_</em></em>
<em><em>_*<p>Hope this helps...</p>*_</em></em>

Here's some code that shows you how to use an array of beasts to draw your toolbar and process the result. The name of the beast will be used as the label on the buttons.

It's still hard to determine from your code, but my guess is that you want your hero to be able to add beasts to his posse from a toolbar. If my understanding is correct, you should be able to use just one array of beasts to accomplish this.

Hopefully this code will help:

/* 
 * An array of beasts that will be used to draw the toolbar.
 *
 * This array could get populated a number of ways.
 * The simplest way for testing purposes would be to drag 
 * and drop GameObjects into the Inspector.
 *
 * I'm using an array of GameObjects because I'm trying to
 * mimmick your code as much as possible.  In all likelihood,
 * you'll want to create a class named Beast, which would 
 * contain a GameObject and some other info about your beasts.  
 * You can find example code to create a class in my first answer.
 *
 */

var beastChoices  :  GameObject [];

function OnGUI () 
{

    var lineupInt     =  -1;

    /* set the string of arrays from the array of GameObjects */

    var lineupStrings =  getBeastNames();

    lineupInt = GUI.Toolbar(Rect(25, 25, 250, 30), lineupInt, lineupStrings);

    /*
     * Now that you have an array of beasts, you don't have to have a bunch of
     * if statements.  You can take the result of the Toolbar selection and 
     * reference the associated beast.
     *
     */

    /* this is how you get the chosen beast */

    var chosenBeast = beastChoices[lineupInt];

    /* now you can write a function that does stuff to the chosen beast */

    doStuffToBeast(chosenBeast);

    /* 
     * OR you could loop over all the beasts in the array and call one
     * function on the chosen beast and a different function on the other beasts.
     *
     */

     for ( var i = 0; i < beastChoices.length; i ++ ) {

        print("Current beast: " + beastChoices*.name);*
 _/* do stuff to the beast chosen in the toolbar */_
 *if ( lineupInt == i ) {*
 _doStuffToBeast(beastChoices*);*_
 _*}*_
 <em>_/* reset all the other beasts */_</em>
 _*else {*_
 <em>_resetBeast(beastChoices*);*_</em>
 <em>_*}*_</em>
 <em>_*}*_</em>
<em>_*}*_</em>
<em>_*function doStuffToBeast ( theBeast )*_</em>
<em>_*{*_</em>
 <em>_*print("Doing stuff to " + theBeast.name + " because it was selected");*_</em>
 <em>_*theBeast.transform.position  =  transform.position + 10;*_</em> 
 <em>_*theBeast.transform.parent    =  transform;*_</em>
 <em>_*theBeast.SetActiveRecursively(false);*_</em>
<em>_*}*_</em>
<em>_*function resetBeast ( theBeast )*_</em>
<em>_*{*_</em>
 <em>_*print("Reset " + theBeast.name + " because it was NOT selected");*_</em>
 <em>_*theBeast.transform.parent = null;*_</em>
 <em>_*theBeast.SetActiveRecursively(true);*_</em>
<em>_*}*_</em>
<em>_*function getBeastNames ()*_</em>
<em>_*{*_</em>
 <em>_*if ( ! beastChoices ) {*_</em>
 <em>_*Debug.Log("Beast array is null!");*_</em>
 <em>_*return;*_</em>
 <em>_*}*_</em>
 <em>_*var theNames = new Array();*_</em>
 <em><em>_/* for each beast in the array, add name to array named theNames */_</em></em>
 <em>_*for ( var i = 0; i < beastChoices.length; i ++ ) {*_</em>
 <em><em>_theNames.Push(beastChoices*.name);*_</em></em>
 <em><em>_*}*_</em></em>
 <em><em><em>_/* convert the dynamic array into a builtin array */_</em></em></em>
 <em><em>_*var theBuiltinArray : String [] = theNames.ToBuiltin(String);*_</em></em>
 <em><em>_*return theBuiltinArray;*_</em></em> 
<em><em>_*}*_</em></em>
<em><em>_*```*_</em></em>

Okay- here is the finished result

    static var Beast = new Array ();

    var lineupStrings : String[]=["empty1", "empty2", "empty3", "empty4"];

function OnGUI () {

    var lineupInt = -1;

    lineupInt = GUI.Toolbar( Rect (370,25,370,30), lineupInt, lineupStrings());

     for ( var i = 0; i < Beast.length; i ++ ) {

     if ( lineupInt == i )         
        if (!Beast_.transform.IsChildOf(transform)){ //**see comment**_
 _doStuffToBeast(Beast*);*_ 
 <em><em>lineupStrings _= Beast*.name;*_</em></em>
 <em><em>_*lineupInt = -1;*_</em></em>
 <em><em>_*}else {*_</em></em> 
 <em><em><em>_resetBeast(Beast*);*_</em></em></em> 
 <em><em><em>_*}*_</em></em></em> 
 <em><em><em>_*}*_</em></em></em>
 <em><em><em>_*function doStuffToBeast ( theBeast ){*_</em></em></em> 
 <em><em><em>_*print("Doing stuff to " + theBeast.name + " because it was selected");*_</em></em></em> 
 <em><em><em><em>_theBeast.transform.position  =  transform.position + transform.right * -4 + transform.up *7;_</em></em></em></em> 
 <em><em><em>_*theBeast.transform.parent    =  transform;*_</em></em></em> 
 <em><em><em>_*theBeast.SetActiveRecursively(false);*_</em></em></em>
 <em><em><em>_*}*_</em></em></em>
 <em><em><em>_*function resetBeast ( theBeast ){*_</em></em></em> 
 <em><em><em>_*print("Reset " + theBeast.name + " because it was NOT selected");*_</em></em></em> 
 <em><em><em>_*theBeast.transform.parent = null;*_</em></em></em> 
 <em><em><em>_*theBeast.SetActiveRecursively(true);*_</em></em></em>
 <em><em><em>_*}*_</em></em></em>
<em><em><em>_*```*_</em></em></em>