Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by davidwalmsley · Nov 04, 2014 at 04:34 AM · component

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Nov 04, 2014 at 04:39 AM 1
Share

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.

avatar image Kiwasi · Nov 04, 2014 at 04:56 AM 0
Share

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.

avatar image Kiwasi · Nov 04, 2014 at 04:59 AM 0
Share

You could also consider using events and delegates. The tutorials on the learn section for this are pretty good.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Nov 05, 2014 at 04:14 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

is it possible to rename a script with script? 0 Answers

enabled is not a member of UnityEngine.Component 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges