Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to get an variable for another script at a random prefab

Discussion in 'Scripting' started by Elder_Crivelari, May 2, 2015.

  1. Elder_Crivelari

    Elder_Crivelari

    Joined:
    Nov 9, 2013
    Posts:
    7
    Hi there guys! Sorry, my english is very bad, but i think you can understand this:

    I am making an rpg like diablo, and i am configuring the enemies, but i have and strange problem here
    i have the player, that uses various types of skills. When the player use the skill, the skill travels to the enemy and hits him. But lets say that the fire magic has "10" points of damage, and the frost have "50" points of damage. When the magic hits the enemy, the collision script needs to find the magic by a tag, to identify the magic. But the thing is, that if i have 200 diferent skills, i will need to create 200 tags? is there any way to create only one tag like "PlayerSkill", and in each skill prefab ill put a script with a static var with the "damage" of the spell, and when the skill hits the enemy, the "collision object" of the enemy script will get that specif variable? so if i have 200 skills with an variable, or property called "damage", every time something hits that skill prefab , it will find the variable value and use it?

    Sorry if im not clear enougth, ill try to explain better if you need! Thanks a lot !
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    "the collision script needs to find the magic by a tag, to identify the magic. "

    This is not the "correct" way of doing this. You should make a magic class of which other spells inherent. That way all the objects are of the same type and you can still make them all do different things (which you specify within the respective classes).
     
    Elder_Crivelari likes this.
  3. Elder_Crivelari

    Elder_Crivelari

    Joined:
    Nov 9, 2013
    Posts:
    7
    Thats the point, i cant figure out how to do this. The "magic" class structure need to be like what? Im new to c# scripting.
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Classes and inheritance are more advanced topics. If you are new to scripting, I would suggest doing some tutorials until you get to these topics and then try to implement them.
     
  5. Elder_Crivelari

    Elder_Crivelari

    Joined:
    Nov 9, 2013
    Posts:
    7
    Ok , thanks a lot! i found it at the unity scripting tutorials, i think now i can make my way through it!
     
  6. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Inheritance and polymorphism are kind of a big deal in OOP. I'd look into both of those things :) Best of luck to you!