using UnityEngine;
public class Numbers: ScriptableObject
{
public AudioClip number_1;
public AudioClip number_2;
public AudioClip number_3;
public AudioClip number_4;
public AudioClip number_5;
public AudioClip number_6;
public AudioClip number_7;
}
My Function script is here:
public void i_see_enemy(string name,string gender,string status,string eclass,string story,int damage,int health) {
Debug.Log($"{name} here, {gender} is {eclass}. {gender} has {damage} damage, {health} health.{story}");
string newFile = "numbers.number_" + damage;
if(!audioSource.isPlaying)
audioSource.PlayOneShot(numbers.number_1);
}
The problem is starting after trying to fetch data from numbers class in PlayOneShot parameter =>
number_1 an example of value name, the newFile is exactly what I want to do. As you can see the fileName is depends on damage and I want to save newFile name as current damage relevant to AI mechanism then fetch inside of numbers list
for example my damage is 5 and I want to call numbers.number_5. However I cant.I cant put newFile string value instead of number_1 after dot.
Please help me If you know thank from now.
Hey there,
first up, we are not on youtube and there is no need for clickbait. Everyone posting questions on here is in search for an answer, here there is no such thing as an “EMERGENCY”.
That being said: What you are trying to do can be done using Linq and reflection. However this is mostly a bad idea and there are better ways to do this. Namely if you change your scriptable object to be this:
using UnityEngine;
public class Numbers: ScriptableObject
{
public AudioClip[] numbers = new AudioClip[8];
}
then you can simply say:
public void i_see_enemy(string name,string gender,string status,string eclass,string story,int damage,int health) {
Debug.Log($"{name} here, {gender} is {eclass}. {gender} has {damage} damage, {health} health.{story}");
if(!audioSource.isPlaying)
audioSource.PlayOneShot(numbers.numbers[damage]);
}
This assumes that “numbers” is already a variable in your class where you loaded an instance of your scirptable object. (A more describing name will help you a lot in the future - perhaps something like “SoundContainer_OnDamage” instead of “Numbers”) If that is not the case please add more description to your question.
Note however that you might want to add a check if
- the AudioClip array has enough entries → number.length > damage
- the AudioClip is not null
Lastly please note that there is a code formatting button over the text field. Please use this next time.