Here is an quick example of what i am trying to accomplish in js for unity.
im trying to retrieve a variable in another object by GetComponent() function. However, i am trying to point to the variable with a string value inside of another.
//separate script contains the desired variable
//lets call it FoodStorageScript inside of FoodObject
var fruit : String = apple;
var vegetable : String = carrot;
//This is the script i’m trying reference the wanted value to.
//This is a separate object and separate script(same folder)
var typeFood : String = "fruit";
var foodScript : FoodStorageScript = FoodObject.gameObject.GetComponent(FoodStorageScript);
var chosenFood : String = foodScript[typeFood];
I’m trying to use “[ ]” these brackets in hopes that is will use the value inside the variable as the name. I sadly have always used this syntax with javascript and php. However, since looking around the forums I now know that javascript syntax in unity is not necessarily the same. When i attempt to do this i get an error stating: ! Type ‘foodScript’ does not support Slicing
Is there a way for me to use the string value in a variable behind the dot syntax instead of the literal name? Thanks for any help in advance. I hope I explained my problem with clarity.
Sounds like you’re going to be wanting reflection here:
import System.Reflection;
var fieldName : string = "fruit";
var myFieldInfo : FieldInfo = typeof(FoodStorageScript).GetField(fieldName);
if (myFieldInfo == null)
{
Debug.Log("Field \"" + fieldName + "\" does not exist!");
}
else
{
var value : object = myFieldInfo.GetValue(FoodObject);
Debug.Log("The value of field \"" + fieldName + "\" is: \"" + value + "\"");
}
That code is off the top of my head. Though typically there’s ways around this to avoid the slow reflection: what is it that you’re trying to accomplish?
Technically your method of Reflection works. However, instead of string variables, I am using AudioClip variables. Currently i’m just going to attempt another method.
But if you can help further, I have Several Instrument sounds located on an empty object. I am trying to play those audio clips from another script when a trigger occurs. I could probably just put the sounds on the same object. But i was hoping to have different empty clips that hold certain instruments (since i am dealing with a large number of sound files). Putting 40 or more sound components along with others would not seem very efficient (the cluster would get to me).
To Sum it up: I want A script to pull a variable from the trigger object and reference it to another object that has the audio clips to play. Hope it makes sense.
I also thought that keeping my audioclips in different objects would could help loading performance later on.
I failed to clarify that i am going from a String variable to an AudioClip
Why don’t you use class properties for this? Define a simple script that uses the kind of sound categories/events you want:
public class SoundSet : MonoBehaviour
{
public var Theme : AudioClip;
public var Success : AudioClip;
public var Error : AudioClip;
public var Discovery : AudioClip;
}
Then create multiple instances of this script on different objects. Name each object with the different instruments you have (say, “Trumpet”, “Choir”, “Drums”) and assign the appropriate audio clips. Then reference which of those you want in your code:
var sounds : SoundSet = GameObject.Find("Trumpet").GetComponent.<SoundSet>();
var source : AudioSource = myAudioSource;
source.clip = sounds.Theme;
source.Play();
Thanks so much. Thats exactly what I needed. I swear i’m thinking of so many different ways to go about doing this. So many reworks. But you sir have helped me much. Thanks again.