Why my method doesn't work

So the first SetActive works perfect, but the second doesn’t work at all -_- Do you know why?

private void Start()
{
    gameObject.SetActive(false);
    fireCooldownHolder = fireCooldown;
    cooldownHolder = cooldownTime;
    activeTimeHolder = activeTime;
}

//активное состояние и перезарядка
enum AbilityState
{
    active,
    cooldown
}
AbilityState state = AbilityState.active;

public override void Activate() => gameObject.SetActive(true);

what second one? you never call it here

1 Like

Line 17.

Question is, what is supposed to be calling Activate()? That’d be where they should start debugging.

As I said, its not called, so there is no second one running…

1 Like

it is called in another script, but unity debug says it’s null exeption

OK you call it a prefab - did you instantiate the flame bullet? whateverr line 35 is in your flamethrower that is the code that has a problem, and its telling you something is not set or created…

1 Like

It’s set, so the problem is: The flameThrower works as it supposed. But i want it to unactive until i call Activate(). The first line - setActive(false) - works as i want. But i get the Null Exception when using Activate().

Well there is no “flame” in your heirachy, so, what exactly are you thinking is set? you assigned a prefab to the field perhaps, its clearly not working as its supposed to or you wouldnt be here :smiley:

So, You need to share that code and enough of it for us to follow what you’re doing…

You need to remember a prefab is a recipie almost it is not something in your scene

The gameObject creates a bullet (the prefab u talking about)
Bullet prefab code:

public class Bullet : MonoBehaviour
{
    public GameObject hitEffect;
    public float animationTime;
    public float damage;

    public float timerToDestroy;

    private Vector3 scaleHolder;

    private void FixedUpdate()
    {
        timerToDestroy -= Time.deltaTime;
        if (timerToDestroy < 0 )
        {
            Destroy(gameObject);
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.TryGetComponent<Enemy>(out Enemy enemyComponent))
        {
            enemyComponent.TakeDamage(damage);
            //Debug.Log("Bullet deals dmg");
        }
        GameObject effect = Instantiate(hitEffect, transform.position, Quaternion.identity);
        Destroy(effect, animationTime);
        Destroy(gameObject);
    }

    public void GetBiggerProjectile()
    {
        Vector3 scale = new Vector3(1f, 1f, 0f);
        gameObject.transform.localScale += scale;
    }

    public void SetToDefault ()
    {
        gameObject.transform.localScale = scaleHolder;
    }

    public void GetDefault()
    {
        scaleHolder = gameObject.transform.localScale;
    }
}

But the problem not in there: I try to activate the gameObject via Activate() but there’s NullEcxeption. However, the gameObject is a reference to a gameobject is attached to. Thus, i can’t understand how

Full flameThrower code:

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

public class FlameThrower : Ability
{
    public FlameBullet FireBulletPrefab;
    public float damage = 3f;
    public float speed;

    public float cooldownTime = 1f;
    private float cooldownHolder;
    public float activeTime;
    private float activeTimeHolder;

    public float fireCooldown;
    private float fireCooldownHolder;

    private void Start()
    {
        gameObject.SetActive(false);
        fireCooldownHolder = fireCooldown;
        cooldownHolder = cooldownTime;
        activeTimeHolder = activeTime;
    }

    //активное состояние и перезарядка
    enum AbilityState
    {
        active,
        cooldown
    }
    AbilityState state = AbilityState.active;

    public override void Activate() => gameObject.SetActive(true);

    // активация со встроенным контролем скорострельности
    public void Shoot()
    {
        if (fireCooldown <= 0f)
        {
            Debug.Log("Shoot");
            FlameBullet fl_bullet = Instantiate(FireBulletPrefab, gameObject.transform.position, gameObject.transform.rotation);
            Rigidbody2D rb = fl_bullet.GetComponent<Rigidbody2D>();
            rb.AddForce(gameObject.transform.up * speed, ForceMode2D.Impulse);

            fireCooldown = fireCooldownHolder;
        }
    }

    

    private void FixedUpdate()
    {
        switch (state)
        {
            case AbilityState.active:
                Debug.Log("State = active");
                fireCooldown -= Time.deltaTime;
                activeTime -= Time.deltaTime;
                if (activeTime > 0)
                {
                    Shoot();
                }
                else
                {
                    activeTime = activeTimeHolder;
                    fireCooldown = fireCooldownHolder;
                    state = AbilityState.cooldown;
                }
            break;
            case AbilityState.cooldown:
                if (cooldownTime > 0)
                {
                    cooldownTime -= Time.deltaTime;
                }
                else
                {
                    state = AbilityState.active;
                    cooldownTime = cooldownHolder;
                }
            break;
        }
    }

}

The method is called properly, but unity says that gameObject is not a reference to a gameobject (only in the Activate()), which is wrong

This statement only means that you did not successfully complete Step #1 of the three-step NullReferenceException checklist.

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

NullReference is the single most common error while programming. Fixing it is always the same.

Some notes on how to fix a NullReferenceException error in Unity3D:

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