Search Unity

Getting information from Scene gameobjects fro prefab uninstantiated

Discussion in 'Scripting' started by Darkness-Seeker, Feb 1, 2017.

  1. Darkness-Seeker

    Darkness-Seeker

    Joined:
    Jun 18, 2016
    Posts:
    11
    Good Day, and first thing, excuse for my low english level.

    These last days i've been following this series of tutorials to create a "simple" Turn Based Combat System in Unity:



    Everything works fine, but now I want to make the Magic system to make it a but more Dynamic.
    The part of "Magic code" that I'd like to modify goes mostly like this:

    - Foreach spell or skill, there's a prefab that holds within a script, which is the spell, that receives all the variables from a "BaseAttack" Script.
    - When the player selects the ability with his corresponding button, it looks for the prefab and inside it, all the info required for the skill in the script, without need of instantiating that script, and automatically takes the damage and launches it to the enemy.

    Right now, the spell code is very simple:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FireSpell : BaseAttack
    5. {
    6.     public FireSpell()
    7.     {  attackName = "Fire 1";
    8.         attackDescription = "Basic Fire spell";
    9.         attackDamage = 20f;
    10.         attackCost = 10f;      
    11.     }  
    12. }
    As I would like it to work, at least if I'm not wrong with the logic, it should go like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FireSpell : BaseAttack
    5. {
    6.     private BattelStateMachine BSM;
    7.     private GameObject performer;
    8.     private HeroStateMachine HSM;
    9.     public FireSpell()
    10.     {
    11.         GameObject performer = GameObject.Find(BSM.PerformList[0].Attacker);
    12.         HeroStateMachine HSM = performer.GetComponent<HeroStateMachine>();
    13.  
    14.         attackName = "Fire 1";
    15.         attackDescription = "Basic Fire spell";
    16.         attackDamage = (performer.GetComponent<BaseHero>().intellect + 20f) - BSM.PerformList[0].AttackersGameObjectTarget.GetComponent<EnemyStateMachine>().enemy.CurrDEFMag;
    17.         attackCost = 10f;      
    18.     }  
    19.  
    20.     void Start()
    21.     {
    22.         BSM = GameObject.Find("BattleManager").GetComponent<BattelStateMachine>();
    23.     }
    24. }
    The Main problem is, as the prefab is never instantiated in the scene, the Start void, and any other void i've think of, isn't able to get the gameobjects from the very moment the script is called by the button.

    Do you know any way I can work around with my idea?
    I am sorry if my explanations are vague or the problem is just too big, but even a simple direction would be welcome.

    Thanks for the attention.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I would not store data in a prefab. If all you want is data, there are better ways using json, xml, a standard text file, or even just a script in the scene that has all the data in it. (and many other ways) but honestly, doing it from a prefab is not really the right use for a prefab. There are many ways that are better for this.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148