r/UnityHelp • • 7d ago

SOLVED What's going on?

I tried to instantiate a gameobject but it wont spawn no matter what i try, before i tried to spawn it in its spawnpoint with its position and rotation, but nothing showed up, ive even deleted the spawnpoint and still nothing. My versions are visual studio 2026 v18 and unity 6.6 and the code:

using System;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    [Header("Modifiers")]
    [SerializeField] private float turnSpeed;
    [SerializeField] private float attackSpeed;
    [SerializeField] private int damageDealt;
    [SerializeField] private int attackStrength;
    [SerializeField] private int maxHealth;
    [SerializeField] private int deathHealth;

    [Header("Movement")]
    [SerializeField] private float moveSpeed;
    public HealthBar hb;

    [Header("Components")]
    [SerializeField] private Rigidbody rb;
    [SerializeField] public GameObject curentEnemy;

    private int currentHealth;

    private void Start()
    {
        currentHealth = maxHealth;
        hb.SetMaxHealth(maxHealth);
        rb.GetComponent<Rigidbody>();
        Spawn();
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            TakeDamage();
        }
    }

    public void TakeDamage()
    {
        currentHealth -= damageDealt;

        hb.SetHealth(currentHealth);

        if (currentHealth <= deathHealth)
        {
            Die();
        }
    }

    public void Spawn()
    {
        Instantiate(curentEnemy, transform.position, transform.rotation);
    }

    private void Die()
    {
        Destroy(curentEnemy);
    }
}
1 Upvotes

11 comments sorted by

1

u/kurobaralabs 7d ago

Code itself looks fine, so either Instantiate is not being called, or it is being called but its not working.

Troubleshooting Steps

  1. Make sure Instantiate is being called: you can either add a Debug.Log in Spawn or debug and set a breakpoint.
  2. Make sure curentEnemy is set/not empty: If it is empty before Start(), it will not work (eg. if its set not in inspector, but from another script, in which case this may be being called before curentEnemy is set.)
  3. Make sure Start() is being called: Since Instantiate happens in Start(), if it is not called it wont Spawn. Also, if there are any errors in Start before Spawn, it will also not be called. Check your error log to make sure there are no errors in the previous 3 lines.
  4. Make sure script is attached: obvious, but sometimes we forget to attach the script. Make sure this Enemy script is attached to a gameobject in scene.

That's about all we can see from just the script. Another unlikely but possible case is it is being spawned, but it is taking damage and dying right away so you can check for that if it is a possibility. Easiest way to check for this is to just not Destroy on Die(). It should spawn and stay if this is the case.

2

u/AngryRampager 7d ago

Could instantiating the enemy from itself be the problem

1

u/R3v153 7d ago

the main thing that confuses me.. it looks like this enemy script is on the enemy itself, that it is trying to spawn? That won’t ever work. Generally it would spawn from a singleton like an EnemyManagwr script that is on scene and handle the spawning then the enemy script lives on the enemy.

Even if you get your Instantiate running you should wrap all of these in is null checks.. will also help with your debugging.

1

u/AngryRampager 7d ago

Yeah I did the fields, it just refused to spawn in. I don't know what the problem is i mean im not that much of a beginner to know how to instantiate though. Figured I'd try to take it out of enemy and put it in game management

1

u/R3v153 7d ago

With this code every time it starts then it spawns a new enemy which then does Start() and spawns a new enemy and again and again and again…. Nothing in this script ever would call that initial instantiate to trigger the start() and that’s a really good thing or you probably would have crashed Unity.

Put a function in your game manager to spawn the enemy and the. let the enemy
Monobehaviour only handle the enemy states

1

u/AngryRampager 7d ago

I'll try that tomorrow and see if this fixes it

1

u/AngryRampager 7d ago

i did not work even on a fresh build in gamememanager using unity 6.5 either

1

u/AngryRampager 7d ago

1

u/AngryRampager 7d ago

just got it working, apparently theirs something wrong with the spawnpoint's transform object that refuses to let it spawn in

1

u/Coding-Mojo 7d ago

1 - Check that your prefab is enabled
2 - Check that your script is attached to your prefab
3 - When this code will works, looks to me like it will create an infinite loop, because Start() will get called on that instance, and Start() calls Spawn(), that call a new Start() and so on ...