- Home /
Question by
piecewise13 · Apr 27 at 03:47 PM ·
c#prefabheirarchy
Multiple instances of prefabs under one parent not working individually
I have multiple prefabs that I dragged and dropped in my scene that each has its own functionality and doesn't interact with each other. The objects are walls that can take damage and eventually will be destroyed if it loses health. When I have my hierarchy ordered like this:
Prefab (1)
Prefab (2)
Prefab (3)
...
It works but when it's ordered like this
EmptyGameobject
Prefab (1)
Prefab (2)
Prefab (3)
...
With EmptyGameobject being the parent of the prefabs, it no longer works. Every time any of the walls get damaged, Prefab (1) will be the one that is destroyed even if it hasn't taken any damage at all. Here's my code for the wall:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class WallDefenceScript : MonoBehaviour, Damageable
{
public ResourceType type;
public float cost;
public float repairAmount;
private PlayerScript player;
public float timeDelay;
public float rebuildDelay;
private float lastRepairTime;
public float health { get; set; }
public float maxHealth;
public bool isDead { get; set; }
[SerializeField]private GameObject wallMesh;
public static NavMeshSurface navSurface;
private NavMeshObstacle obstacle;
// Start is called before the first frame update
void Start()
{
health = maxHealth;
navSurface = FindObjectOfType<NavMeshSurface>();
obstacle = GetComponentInParent<NavMeshObstacle>();
}
public void takeDamage(float damage, Collider hitCollider)
{
if (!isDead)
{
health -= damage;
if (health <= 0.0f)
{
death();
}
}
}
public void death()
{
wallMesh.SetActive(false);
navSurface.BuildNavMesh();
obstacle.enabled = false;
isDead = true;
}
}
Comment
Your answer
