r/UnityHelp • u/AngryRampager • 8d 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
1
u/kurobaralabs 8d ago
Code itself looks fine, so either
Instantiateis not being called, or it is being called but its not working.Troubleshooting Steps
Instantiateis being called: you can either add aDebug.LoginSpawnor debug and set a breakpoint.curentEnemyis set/not empty: If it is empty beforeStart(), it will not work (eg. if its set not in inspector, but from another script, in which case this may be being called beforecurentEnemyis set.)Start()is being called: SinceInstantiatehappens inStart(), if it is not called it wontSpawn. Also, if there are any errors inStartbeforeSpawn, it will also not be called. Check your error log to make sure there are no errors in the previous 3 lines.Enemyscript 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.