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 Charva1976 · Apr 27, 2013 at 12:51 AM · generic

How do I access a specific component in a generic way?

So I'm mocking up a turned based rpg combat system. My plan is to store each of the members of the combat in an array and then using a sort function of the array based on speed to figure of each member's turn. Since each mob and PC will have unique attacks etc, each will have a unique animation script. The problem I'm running in to is that I can't find a way to reference each member's script in a generic way. I want to be able to call "attack #3" on the animation script and have that character's unique attack fire. I tried adding the name of each Animation script to the base class for each as a string, then tried to look up that name from the getcomponent("string") but I get errors.

 //create mobs for testing
     robo = new CharacterAttributes(Player.Robo, "Robo", CombatPosition.PlayerFrontCenter, "Combat Robo" , "RoboAnimationController" ,20,21,19,20,18,22,22,57000);
     robo.AddWeapon(new Weapon("Ion Gun", 1, 100, false, Element.physical, 100, Player.Robo));
     var monster : NPCAttributes = new NPCAttributes("Psy Auto", "Psy Auto", "PsyAutoAnimationController", 8, 200, 20, 30, 1, 1, 5, 1, 15, false, 50);
 
     //populate the battle field
     robo.myInstance = Instantiate(robo.model, Vector3(combatPositions[robo.combatPosition].x, (robo.model.collider as BoxCollider).size.y/2,combatPositions[robo.combatPosition].z), Quaternion.Euler(0,0,0));
     monster.myInstance = Instantiate(monster.model, Vector3(combatPositions[8].x,(monster.model.collider as BoxCollider).size.y/2,combatPositions[8].z), Quaternion.Euler(0,180,0));
 
 
     while(true){
         //create a loop that loops through party and monsters and returns the highest initiative TBA
         
         //Select next attacker based on return TBA
     
         //need to call a menu here with options for player on party turn populated with options for each character individually TBA
 
         yield robo.myInstance.GetComponent(robo.myAnimationScript).AttackTarget(robo, monster);// Here I get errors that .AttackTarget isn't a component of UnityEngine.Component 
         
         if(gameMasterController.inCombat == false) break;

Any ideas. I'm pretty new to OOP, but I'm learning fast.

Cheers

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Chronos-L · Apr 27, 2013 at 01:16 AM

// Here I get errors that .AttackTarget isn't a component of UnityEngine.Component

Look at this, DataCheck.js line 15. There are few ways to write a proper GetComponent() in the same answer, pick the one you want to use.


You can try inheritance to improve the code, this is a C# example, I can't write in uJS anymore:

BasicAttack.cs

 public abstract class AbstractAttack : MonoBehaviour {
    public abstract void Attack( Enemy e );
 }

SwordAttack.cs

 public class SwordAttack: AbstractAttack {
    public override void Attack( Enemy e ){
       ...
    }
 }

MagicAttack.cs

 public class MagicAttack: AbstractAttack {
    public override void Attack( Enemy e ){
       ...
    }
 }

Then, you can just use:

 someone.GetComponent<BasicAttack>().Attack( targetedEnemy );

This will call the Attack() in the SwordAttack,MagicAttack, or any classes that extends the AbstractAttack, this depends on what you attach to someone. So you can have 30 attack scripts, but you can access them all using the AbstractAttack.

It will work in uJS, but I can't write in uJS anymore; you will have to figure that out yourself.


Reference link:

http://answers.unity3d.com/questions/438300/returning-variables-from-an-inherited-class.html

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 Charva1976 · Apr 28, 2013 at 03:07 PM 0
Share

Thank you very much. I've been avoiding rewriting my code to embrace inheritance. This will give me the kick to convert over to C# and rework my logic.

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

13 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

Related Questions

Public List of Scripts? 1 Answer

Generate Chart with Data 0 Answers

Fixing a single call of an action (across 2 scripts) 1 Answer

i get errors but i have no idea why 0 Answers

Can I use generic objects as variables then cast them upon construction? 2 Answers


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