• 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 /
  • Help Room /
avatar image
0
Question by Bulytc · Feb 06, 2016 at 06:11 PM · c#scripting problemreferencerpgrpg-game

Referencing instances of scripts based on the GameObject they are attached to.

Hi. I am fairly new to Unity and I am trying to code the State Machine for a turn-based RPG battle system. I'm trying to make it so I can use it in basically every battle throughout the game, so I'm keeping it as generic as possible. The StartBattle() function is called by a OnTriggerEnter() function when the player touches the monster. The battle system code is as follows:

 using UnityEngine;
 using System.Collections;
 using  System.Collections.Generic;
 
 public class BattleSystem : MonoBehaviour {
     List<GameObject> heroes = new List<GameObject>();
     List<GameObject> enemies = new List<GameObject>();
 
     public enum turns{hero, middle1, enemy, middle2};
     private turns currentTurn;
 
     public int i = 0;
 
     public Dictionary<string, System.Action> battleDictionary = new Dictionary<string, System.Action>(){
         {"Player", PlayerFight.Fight}
         ,   {"Imp", ImpFight.Fight}
         ,   {"Rat", RatFight.Fight}
     };
 
     // Use this for initialization
     void Start () {
         currentTurn = turns.hero;
 
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     public void StartBattle(List<GameObject> friends, List<GameObject> monsters){
         foreach (GameObject friend in friends) {
             heroes.Add(friend);
         }
         foreach (GameObject enemy in monsters) {
             enemies.Add(enemy);
         }
 
         heroes.Add (null);
         enemies.Add (null);
 
         RealBattle ();
     }
 
     public void RealBattle(){
         //=========================Heroes turn==============================
         if (currentTurn == turns.hero) {
             if (heroes [i] != null) {
                 //call fight function for every hero using the dictionary (the function has to end with BattleSystem.i++; BattleSystem.RealBattle();)
                 battleDictionary[heroes[i].tag]();
             } else {
                 i = 0;
                 currentTurn = turns.middle1;
                 RealBattle ();
             }
         }
         //=========================Middle1 turn==========================================
 
         else if (currentTurn == turns.middle1) {
         //do atks, calculate damage, check if all monsters are dead. If not:
             currentTurn = turns.enemy;
             RealBattle();
         }
         //=========================Enemies turn==========================================
         else if(currentTurn == turns.enemy){
             if (enemies [i] != null) {
                 //call fight function for every enemy using the dictionary (the function has to end with BattleSystem.i++; BattleSystem.RealBattle();)
                 battleDictionary[enemies[i].tag]();
             } else {
                 i = 0;
                 currentTurn = turns.middle2;
                 RealBattle ();
             }
         }
         //=========================Middle2 turn==========================================
         else if (currentTurn == turns.middle2) {
             //do atks, calculate damage, check if all heroes are dead. If not:
             currentTurn = turns.hero;
             //RealBattle();
         }
     
     }
 }

This code works, but poorly. That is, the Fight() functions need to be static, because I can only reference the class and not the specific instance of the script attached to that GameObject (e.g. I'm not able to reference the ImpFight attached to Imp1, only the whole ImpFight class). That, of course, is really problematic because I can't have more than one monster that uses a script from that class on the same battle, as the commands would apply to both. What I'm looking for is a way to reference the instance of that script specifically but with the code still being generic so I can use it throughout the whole game. Thanks and sorry for broken english.

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Hi everyone.need help with this. 1 Answer

Instatiated object not being referenced in Start function? 2 Answers

Getting Int value from script to other script 2 Answers

RPG instantiate problem 3 Answers

RPG Class Selection Menu 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges