• 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
1
Question by Paniku · Apr 21, 2013 at 05:52 AM · instance

Problem with instances of class

Certain "skills" in my game have buffs. When a Monster() attacks with a skill, an attack command is sent with the target's Monster() instance and the attacker's Monster() instance. Obviously, the damage is sent to the target and the buffs are sent to the attacker.

For some reason though, the buffs are added to the target and I have no idea why. Adding debug logs confuses me even further because it actually confirms that the buff is being sent to the attacker, not the target (or so I assume).

Here's the script for the skill:

 public class FistoftheBoar : Skill, IEffect
 {    
     public FistoftheBoar()
     {
         Name = "Fist of the Boar";
         Desc = "An inaccurate attack capable of dealing devastating damage. Boosts attack for 3 turns.";
     }
     
     public void ApplyEffect(Monster target, Monster sender)
     {
         target.Damage(2, DamageType.Physical);
         sender.AddBuff(new SkillBuff("Fist of the Boar", BuffType.DMG, 2.0f, 3));
     }
 }

And the script for the Monster class:

 using UnityEngine;
 using System.Collections.Generic;
 using System.Linq;

 public class Monster : MonoBehaviour
 {
     public string Name = "Hercules";
     public int MaxHealth = 100;
     public TextMesh HealthIndicator;
     
     public Skill[] AvailableSkills = new Skill[3];
     
     private Dictionary<SkillBuff, int> _buffs = new Dictionary<SkillBuff, int>();
     private int _currentHealth;
     
     void Start()
     {
         _currentHealth = MaxHealth;
         HealthIndicator.text = _currentHealth + "/" + MaxHealth;
         
         AvailableSkills[0] = new CrushingBlow();
         AvailableSkills[1] = new VictorySlam();
         AvailableSkills[2] = new FistoftheBoar();
     }
     
     public void Damage(int amount, DamageType dmgType)
     {
         float modAmount = amount;
         SkillBuff[] dmgBuffs = SkillHelper.FindBuffs(_buffs.Keys.ToArray(), BuffType.DMG);
         foreach (SkillBuff dmgBuff in dmgBuffs)
         {
             modAmount *= dmgBuff.Modifier;
         }
         
         _currentHealth -= (int)modAmount;
         HealthIndicator.text = _currentHealth + "/" + MaxHealth;
         
         BattleManager.Instance.ShowDamage(transform.position, (int)modAmount);
     }
     
     public void AddBuff(SkillBuff buff)
     {
         if (_buffs.ContainsKey(buff)) _buffs[buff] = buff.TurnTime;
         else _buffs.Add(buff, buff.TurnTime);
     }
     
     public void Attack(Monster target, Skill skill = null)
     {
         if (skill == null) skill = AvailableSkills[Random.Range(0, AvailableSkills.Length)];
         if (skill is IEffect) ((IEffect)skill).ApplyEffect(target, this);
     }
     
     public void ModifyBuffTime(int amount, SkillBuff modBuff = null)
     {
         if (modBuff != null && _buffs.ContainsKey(modBuff)) _buffs[modBuff] += amount;
         else
         {
             SkillBuff[] currentBuffs = _buffs.Keys.ToArray();
             foreach (SkillBuff buff in currentBuffs)
             {
                 _buffs[buff] += amount;
                 if (_buffs[buff] <= 0) _buffs.Remove(buff);
             }
         }
     }
     
     public int GetHealth() { return _currentHealth; }
     public Dictionary<SkillBuff, int> GetBuffs() { return _buffs; }
 }

I apologize that it's uncommented, but hopefully someone could help me with this problem. It's really frustrating and I have no idea what's wrong.

Comment
Add comment · Show 5
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 whydoidoit · Apr 21, 2013 at 07:55 AM 0
Share

I'm taking a look - one comment on style though - (IEffect) skill is a slower cast than (skill as IEffect)

avatar image whydoidoit · Apr 21, 2013 at 07:56 AM 0
Share

Oh and all of that is slower than doing:

   var skillEffect = skill as IEffect;
   if(skillEffect != null)
avatar image fafase · Apr 21, 2013 at 08:02 AM 0
Share

The answer might be in how you call the ApplyEffect. It could be something you do before the call making target and sender being two references to the same object (the target).

avatar image whydoidoit · Apr 21, 2013 at 08:02 AM 0
Share

It would certainly appear that the monster had to be attacking itself for that to happen. You've debug.logged the target and the sender and they are different you said?

avatar image whydoidoit · Apr 21, 2013 at 08:03 AM 0
Share

@fafase snap

1 Reply

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

Answer by Paniku · Apr 21, 2013 at 12:14 PM

Argh! I found the problem! On the Damage() method, the buffs were being taken from the target rather than the sender. So, if the monster1 attacked monster2, monster2's buffs would affect the attack rather than monster1's.

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

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

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

Unity fails to recognize Maya models/Unity scenes 1 Answer

Weird gameplay slow down problem 1 Answer

A node in a childnode? 1 Answer

Unity and Ports : Networking 1 Answer

I want to develop responsive resolution game for all device 2 Answers

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