- Home /
What's the best way to access a component that isn't pre-defined?
I am trying to create a system that casts magic abilities for my combat system. In a nutshell I want my main combat script to be able to call a MagicStart(); function that will be applied to one of a few different magic ability scripts. To make the combat script call the function from the different script, I need to declare the variable at the beginning which is then assigned to corresponding component. The issue is that when I declare the variable I need to put the specific name of the component after it, so say I have three different scripts I want to be able to call from the combat script, I can only call one of them. Is there a way to make it so that the function I call from the combat script can be assigned a different component?
for example :
var magicCast : not sure what would go here
function StartMagicCast(){
magicCast.MagicStart();
}
I have tried using SendMessage to accomplish this, but I have issues with the preciseness that it executes the Start functions. I have also tried to make a class EventListener with a boolean, attach it to the main combat script and all the scripts I want to have for magic cast scripts and make the combat script Listener equal to the magic class Listener variable that I want to use and make the Listener bool true on both scripts, then the magic cast script has:
if(EventListener){
MagicStart();
EventListener = false;
}
If that makes any sense. I feel like this sloppy and am hoping there is a better more simple way to do it. Basically I want to have a x number of magic spell components that have very different code for their functionality and yield timers that use multiple different particle systems, but can all be called from the same function in my combat script. Is this possible? Is there a better way to it that my roughshod EventListener method? Thanks for taking the time to read this.
Take a look at @asafsitner's answer here:
http://answers.unity3d.com/questions/381529/how-to-address-scripts-without-knowing-their-names.html
I typically use a base class.
The answer is pretty good. I tend to use interfaces, I find base classes get a bit messy. But that is all down to personal preference and the exact use case, I've seen others complain that interfaces get a bit messy.
You could also consider using events and delegates. The tutorials on the learn section for this are pretty good.
Answer by davidwalmsley · Nov 05, 2014 at 04:12 AM
!!!
Delegates was EXACTLY what I was looking for! I haven't taken the time to learn C# (however the more I learn about scripting the more Im feeling the move over to C# is going to be inevitable) but the var : Function in unity script is exactly what I was looking for for. Thanks BoredMormon.
//the spell manager Code
var StartSpell : Function;
//bunch of leading up to the call
StartSpell();
//The Spell Code
var spellManager : spellManager;
spellManager.StartSpell = BeginCast;
function BeginCast(){
//all the code to execute the spell
}
I'm way more excited about this than I should be. I haven't gotten such a definite answer in quite some time.
Glad I could help. I remember the exact same feeling the first time I started to understand the potential of delegates and events.
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Get Component that a GameObject is Attached To 2 Answers
Adding joints through script 2 Answers