comparing enums and changing variables

Good day,

I’ve spent about a day and half working with unity after spending several months using UDK and I’m already further than I was with my test game.

So I want to spawn spells that when they collide with other spells they first check the spell type and apply buffs if they are of a certain combination. Than I’d like them to subtract their damage and the weaker of the two reaching a damage of 0 and therefore being destroyed.

I was getting decent results for a while then I added more else if statements to the buff and I get nulls when I hit the floor where I didn’t before. Perhaps there is a better way to do this.

Within the spellcontrller there is an enum of the spell types and the power of the spell:

var spellPower: float = 100;
var spellSpeed: float = 3.0;
var spellType: SpellColor;

enum SpellColor { Red, Blue, Green, Black, White }

Then if the spells touch and, I thought, if they hit the floor they would die.

function OnCollisionEnter ( other : Collision) {

    if (other.gameObject.tag == "Spell")

    {
        Debug.Log("Spells Touched!");

        SpellBuff ();
        
        Debug.Log("Spell Affinityies added!");


        if (GetComponent(spellController).spellPower > other.gameObject.GetComponent(spellController).spellPower) {

            Debug.Log("Spell is bigger!");
            
            GetComponent(spellController).spellPower -= other.gameObject.GetComponent(spellController).spellPower;

        }

        else if (GetComponent(spellController).spellPower < other.gameObject.GetComponent(spellController).spellPower) {

            Debug.Log("Spell is smaller");
            
            Destroy(gameObject);

        }


        else  {

            Debug.Log("Spells cancell");

            Destroy(gameObject);

        }



    }



    else if (other.gameObject.tag == "Floor") {
    
    Debug.Log ("Spell hit the ground and died");
    
    Destroy(gameObject);
    
    }

}

the SpellBuff is :

function OnCollisionEnter ( other : Collision) {

         if ( GetComponent(spellController).spellType == SpellColor.Red  other.gameObject.GetComponent(spellController).spellType == SpellColor.Blue ) {
        
            Debug.Log("This Red spell and that Blue Spell");

            gameObject.GetComponent(spellController).spellPower *= .5;
            
        }
        
        else if ( GetComponent(spellController).spellType == SpellColor.Blue  other.gameObject.GetComponent(spellController).spellType == SpellColor.Black ) {
        
            Debug.Log("This Blue spell and that Black Spell");

            gameObject.GetComponent(spellController).spellPower *= .5;
            
        }


etc  .....

        
        else {
            
            Debug.Log("Spells didn't have affinty!");
            
        }

so it seems the buffs aren’t even applying. Any insight would be greatly appreciated.

This is an error code I receive

You are trying to create a MonoBehaviour using the 'new' keyword.  This is not allowed.  MonoBehaviours can only be added using AddComponent().  Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.MonoBehaviour:.ctor()
SpellBuff:.ctor() (at Assets/Spell Assests/Scripting/SpellBuff.js:1)
SpellsTouch:OnCollisionEnter(Collision) (at Assets/Spell Assests/Scripting/SpellsTouch.js:10)

The error isn’t from any of the code you posted…as it says, you can’t create MonoBehaviours using new, you have to use AddComponent. (In Unityscript, all scripts are automatically a class that inherits from MonoBehaviour.) In other words, they have to be attached to a GameObject.

–Eric

Thank you for the reply, but I’m still at a loss.

When I created the prefabs for the spells, I attached all the scripts via drag and drop. I understand class inheritance from my other OOP studies, but I’m not getting this “new” keyword business. If you could elaborate I’d be much apprechiated. What is this new keyword I made for example. Thanks again.