Programming logic error?

I want to know why I can’t make the comparison in the Chase function of the Action script. The specific comparison is: if (stats.creature == 0) { Debug.Log("Creature Sea Urchin"); } In the Entity script, stats is a ScriptableObject that I created, and creature is a float. The ScriptableObjectStats I referenced in the Entity script has creature = 0.

The Entity and Action scripts are on the same object.

using Unity.Mathematics;
using System;
using UnityEngine;

namespace OnlyRule
{
    public class Action : MonoBehaviour
    {
        //Referenciando os status
        public Entity stats;
        //Referenciando o Rigidbody2D
        Rigidbody2D rb;
        //Referenciando o estado da entidade
        public string state;  
        //Referenciando o obj alvo
        public GameObject target, target_sensor, priority_target;
        public quaternion add_rotation;
        public string ActionName;

        void Start()
        {
            stats = gameObject.GetComponent<Entity>();
            rb = GetComponent<Rigidbody2D>();
            ActionName = stats.Name;
            //Debug.Log("\nname entity: " + stats.Name + "\n speed: " + stats.speed + "\n rigidbody2D: " + stats.heavy);
        }

        // Update is called once per frame
        void Update()
        {
            if(state == "idle")
            {

            }
            if(state == "chase")
            {
                //Verefica se os alvo são validos antes de passar entrar no Chase
                if(target_sensor != null || priority_target != null)
                {
                    if(priority_target == null)
                    {
                        target = target_sensor;
                    }
                    else 
                    {
                    target = priority_target;
                    }
                    Chase(target.transform.position);
                    // ok Debug.Log("target valido");
                }
                //Sai do estado "chase"
                else 
                {
                    state = "idle";
                    target = null;
                }
                // ok Debug.Log("Rodando chase");
            }
        }

        public void Chase(Vector3 v3)
        {   //Movimentação com base na criatura
            // ok Debug.Log("nome atual: " + stats.Name);
            if (stats.creature == 0)
            {
                Debug.Log("Creature Sea Urchin");
            }


            /*if(stats.Name == "Sea Urchin")
            {
                /*if(v3.x < transform.position.x)
                {
                    //mover-se para direita(negativo)
                    add_rotation = quaternion.Euler(0, 0, math.radians(transform.rotation.z) - math.radians(stats.speed));
                    rb.MoveRotation(add_rotation);
                }
                if(v3.x > transform.position.x)
                {
                    //mover-se para esquerda(positivo)
                    add_rotation = quaternion.Euler(0, 0, math.radians(transform.rotation.z) + math.radians(stats.speed));
                    rb.MoveRotation(add_rotation);
                }

                float rotationSpeed = stats.speed;

                // Gira o objeto no eixo Z constantemente
                rb.MoveRotation(rb.rotation + rotationSpeed * Time.deltaTime);
            }*/
            // ok Debug.Log("nome atual: " + stats.Name);
        }
    }
}

using UnityEngine;

namespace OnlyRule
{
    public class Entity : MonoBehaviour
    {
        //Status do obj
        public ScriptableObjectEntityStats stats;
        public InterativeOBJ item;
                        //Status Básicos
        public float    base_hp,
                        base_speed,
                        base_heavy,
                        base_damage,
                        //Status Atuais
                        hp,
                        speed,
                        heavy,
                        damage,
                        //Status Bônus
                        bonus_hp,
                        bonus_speed,
                        bonus_heavy,
                        bonus_damage,

                        creature;
        
        public string Name;
                        
        //Referenciando o Rigidbody2D
        private Rigidbody2D rb;

        void Start()
        {
            rb = GetComponent<Rigidbody2D>();
            //Definindo status
            base_hp = stats.life;
            base_speed = stats.speed;
            base_heavy = stats.heavy;
            base_damage = stats.damage;
            creature = stats.creature;
            Name = stats.name_;
            UpadateStats();
            Debug.Log(Name);
        }

        void Update()
        {
            
        }

        void UpdateSatsBonus(InterativeOBJ stats_bonus)
        {
            if(stats_bonus == null)
            {
                stats_bonus = new InterativeOBJ();
            }
            bonus_hp = stats_bonus.life;
            bonus_speed = stats_bonus.speed;
            bonus_heavy = stats_bonus.heavy;
            bonus_damage = stats_bonus.damage;
        }

        void UpadateStats()
        {
            hp = base_hp + bonus_hp;
            speed = base_speed + bonus_speed;
            heavy = base_heavy + bonus_heavy;
            rb.mass = heavy;
            damage = base_damage + bonus_damage;
        }
    }
}

What do you mean you can’t make the comparison? Is there an error? Is it a compile time error: your IDE shows an error? Is it a runtime error? What does this error say? Is it not an error, but you have some unexpected behavior?

Floating point comparison isn’t done by using the equality operator due to precision errors but I doubt this is the problem in this case.

You don’t give much information about what’s going on, what you expected to happen and what actually is happening other than you can’t make the comparison which could mean a number of different things.

If you’re getting an ambiguity error it is likely because you named your class Action and you’re including the System namespace.

This perhaps puts you in contention for identifier resolution with the System.Action identifier versus your identifier.

Don’t do that. If you must, then learn about namespacing and fully-qualified identifiers.

But in the end, still don’t do that. Don’t confuse your code by reusing names.

Otherwise, you haven’t said what your issue is, so start there.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post code, only post the relevant code and always use the format button above. Do not post photographs of code.

1 Like