r/UnityHelp • u/AngryRampager • Sep 25 '26
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
View all comments
Show parent comment
1
u/R3v153 Sep 26 '26
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