creating a new game object every frame [solved now]

void AttackEnemy(GameObject enemy1)
        {
            float xDiff = enemy1.transform.position.x - transform.position.x;
            float yDiff = enemy1.transform.position.y - transform.position.y;
            float zval = transform.position.z;
            Vector3 attackangle = new Vector3 (xDiff, yDiff, zval);
            // animate
            Debug.Log("AttackEnemy");
            if (Vector3.Angle(Vector3.left, attackangle) >= 0 && Vector3.Angle(Vector3.left, attackangle) <= 45)
            {
                Debug.Log("AttackEnemy");
                animator.Play("HALeft2");
            }
            else if (Vector3.Angle(Vector3.right, attackangle) >= 0 && Vector3.Angle(Vector3.right, attackangle) <= 45)
            {
                animator.Play("HARight2");
            }
            else if (Vector3.Angle(Vector3.up, attackangle) >= 0 && Vector3.Angle(Vector3.up, attackangle) < 45)
            {
                animator.Play("HAUp2");
            }
            else if (Vector3.Angle(Vector3.down, attackangle) >= 0 && Vector3.Angle(Vector3.down, attackangle) < 45)
            {
                animator.Play("HADown2");
            }

            // wait and only run this code that increments damage_taken once every N frames (N = 2 for now)
            // try to actually understand the math
            GameObject attackgo = new GameObject();
            GameObject defendgo = new GameObject();
            GameObject damagego = new GameObject();

            attack_skill_hits attack = attackgo.AddComponent<attack_skill_hits>();
            attack_skill_evades defend = defendgo.AddComponent<attack_skill_evades>();
            swinging_thrusting damage = damagego.AddComponent<swinging_thrusting>();

            if (Time.time > nextActionTime && InAttackRange(transform.position, 50))//do animations separately? - yes
            {
                if (attack.skill_hits(7) && !defend.skill_evades(5, 7, 9))
                {
                    if (swinging)
                    {
                        damage_taken += damage.dmg_swinging(15) + 2;//+ weapon damage modifier + other modifiers(weapon specialization etc.);
                    }
                    else
                    {
                        damage_taken += damage.dmg_thrusting(15) + 1;// + weapon damage modifier + other modifiers(weapon specialization etc.);
                    }
                    Debug.Log(damage_taken);
                }
                nextActionTime = Time.time + period;
            }
        }

currently this code plays every frame that the state is in attackenemy, so the game objects attackgo, defendgo, and damagego are created every frame. the only alternative is to put the game object creation code outside any functions, but that generates an error (which still lets the game run, or at least did once, but i think it’s a problem). if i put the generation code in start or awake i get a “doesn’t exist in the current context” error for attackgo, defendgo, and damagego respectively.

is it such a big deal to create new game objects like crazy (they show up in the editor), or is there some other alternative?

edit: could i just delete some of the objects so that the game isn’t inundated with them? very messy approach

edit2: here’s the solution someone gave me - https://hastebin.com/ilupoqodan.cs

i just assumed the solution in edit2 would work before testing it, but it does not. the code outside any functions gives the same error. is there another way around this, should i ignore the debug, or what?

You will need to explain what you are trying to achieve with this. Whats the issue? Whats the error? Why do you need three new separate gameobjects?

If these are actions of some sort (the defend, attack, damage stuff) this a terrible way of going about it. You can achieve this stuff without instatiating components and gameobjects are runtime using events and delegates & persistant components.

1 Like

i’m instantiating classes for these three things i’m doing with respect to a single attack action (see if the attack exceeds the defense and deal damage if it does). i could put the code in the same class but i was taught that making as many things into individual scripts is the way to go. i’'m still a programmer in training so i might be ignorant of some things that you are taught in school.

Without seeing all your code its hard to tell, but I can guarantee you don’t want to get attack values by creating objects.

1 Like

well the function is in another class, so how else do i do it? basically i have attack.skill_hits, defend.skill_evades, etc., these are the functions i need to use. should i make the class static or something?

They don’t have to be static but if they are just functions that don’t change any data except for what is passed to them they could be. But if they have data that they hold and need to be different at times you can just get a reference to them at the start.

attack_skill_hits attack;
attack_skill_evades defend;
swinging_thrusting damage;

void start()
{
    attack = new attack_skill_hits();
    defend = new attack_skill_evades();
    damage =  new swinging_thrusting();
}

//And then use them as you normally would
if (attack.skill_hits(7) && !defend.skill_evades(5, 7, 9))
 //Do stuff
1 Like

this gives me a “you are trying to create a MonoBehavior using the ‘new’ keyword. …” error. i think it still runs but i get the error. is there a way to get around this error?

edit: that’s the whole reason i used the game objects, to avoid this error. yellow errors don’t seem to crash the game though. should i just go for static then? i guess MonoBehavior complicates things in Unity.

edit2: i guess static is the way to go? i changed it and it works without error.

Oh yea my bad, I forgot. if you need things like Update, start, OnEnable in those three classes they have to be derived from MonoBehavior. To add a Script that is derived from MonoBehavior you need to use add component.
The error should have said something along the lines of “you need to use add component to add this mono behavior”
Make sure you look at the complete error when you get one.

attack_skill_hits attack;
attack_skill_evades defend;
swinging_thrusting damage;

void start()
{
    attack = gameObject.AddComponent (attack_skill_hits);
    defend = gameObject.AddComponent (attack_skill_evades);
    damage = gameObject.AddComponent (swinging_thrusting);
}

//And then use them as you normally would
if (attack.skill_hits(7) && !defend.skill_evades(5, 7, 9))
    //Do stuff
[Code]
1 Like

Yellow is for warnings red is for errors.

1 Like

i think i’m just going to use static for now, but at least now i have this thread for reference (as does everyone else). thank you carl010010!

Yea no problem man. Happy to help.