- Home /
How to reference a public gameobjects in script which are attached in the inspector. (solved)
I'm a sound designer so I'm currently logging debugs for where my event triggers will go. short description of what i'm trying to do:
I have two spells with cool downs and want a different sound for each one when they finish the cool down but the spells use the same "user action" script (pictured) with public variables changed in the inspector like the countdown time or prefab effect.
How can achieve this? i've included some of the script which relates to the ending off the cool down and a commented out part where I attempted to solve it but it kind of describes what I want it to do.
void Update()
{
if (myState == MyState.Cooldown)
{
if (cooldownCounter > 0f)
{
cooldownCounter -= Time.deltaTime;
UpdateCooldownText();
}
else if (cooldownCounter <= 0f)
{
StopCooldown();
Debug.Log("cooldown over but this shows for both spells ");
//If the public gameobject "useractionprefab" in the inspector is "starburst" when the countdown finishes then play that sound
/*if (userActionPrefab = ("StarBurst")
{
Debug.Log("cooldown over for starburst spell");
} */
}
}
}
Answer by JackQuinton · Jun 20, 2017 at 03:42 PM
I figured it out by myself. If there is an easier way of doing this please let me know.
void Update()
{
if (myState == MyState.Cooldown)
{
if (cooldownCounter > 0f)
{
cooldownCounter -= Time.deltaTime;
UpdateCooldownText();
}
else if (cooldownCounter <= 0f)
{
StopCooldown();
Debug.Log("cooldown over but this shows for both spells ");
if (userActionPrefab.name.Equals("FireStorm"))
{
Debug.Log("timer reup for fire storm");
}
Answer by JxWolfe · Jun 20, 2017 at 11:40 AM
void Update()
{
if (myState == MyState.Cooldown)
{
if (cooldownCounter > 0f)
{
cooldownCounter -= Time.deltaTime;
UpdateCooldownText();
}
else if (cooldownCounter <= 0f)
{
StopCooldown();
Debug.Log("cooldown over but this shows for both spells ");
//If the public gameobject "useractionprefab" in the inspector is "starburst" when the countdown finishes then play that sound
// // ask if the userActionPrefab script if is starburst or not
if (userActionPrefab.GetComponent(UserActionPrefab).isStarburst)
{
Debug.Log("cooldown over for starburst spell");
}
else{ //is the other spell
Debug.Log("cooldown over for starburst spell");
}
}
}
}
That would work if you added a public bool called "isStarburst".
Edit for any more then two:
oops, in the brackets you would need the userActionPrefab's script, and if you had more then two spells then i would have an enum and use a switch statement like this small code:
[System.Serializable]
public enum Spells {Starburst,Spell2,Spell3}
//public enum Spells {starburst,spell2, spell3} //so on
//the script that is sending the spell not "Test"
public class Test : MonoBehaviour {
public Spells spells;
then you would get it with a switch statement that is like this:
switch (spells) {
case Spells.Starburst:
{
//is a starburst
//then after you are finished with you code you have to have this line
break;
}
case Spells.Spell2:
{
//is another spell
break;
}
case Spells.Spell3:
{
//so on
break;
}
that code only works from the "Test script" you would have to edit it for the other scripts but that's the idea
Thanks for the help. Would you know how to do this if I had more than two spells?
I copied in your script and added a "public bool isStarburst" but i'm getting these errors. I guessed it could of been because the UserActionPrefab starting with a capital but I still got an error when I changed it. Any ideas?
oops, it is the script you added the bool, so the name of the code on userActionPrefab
Your answer
Follow this Question
Related Questions
NPC Class looking for help 1 Answer
Multiple Cars not working 1 Answer
Cliks per second does not work 1 Answer
Passing a variable to Start() - C# 2 Answers
Distribute terrain in zones 3 Answers