Search Unity

OnTriggerEnter on prefab

Discussion in 'Getting Started' started by thenewkgb82, Nov 28, 2019.

  1. thenewkgb82

    thenewkgb82

    Joined:
    Nov 10, 2018
    Posts:
    52
    [edit] This was not a OnTriggerEnter issue. I cannot change the title

    I have a simple zombie gameobject that I have made into a prefab. I would like several on screen. My first zombie I place works great. When I add a second to the scene it does not work. My zombie script does not have any public or serializedfield variables.
    OnTriggerEnter works for only the first placed zombie but not any more.
     
    Last edited: Nov 29, 2019
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    589
    Hi,
    I doubt the prefab is the issue here. Most likely you got something in your script wrong.
    When you say OnTriggerEnter, you mean an OnTriggerEnter in your zombie?

    Can you share some code so that we can see what the problem might be.
     
    Last edited: Nov 28, 2019
  3. thenewkgb82

    thenewkgb82

    Joined:
    Nov 10, 2018
    Posts:
    52
    Thanks for helping :)
    Oh, the box collider which is set to trigger is placed on the zombie parent that has the script component

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ZombieRaiser : MonoBehaviour
    6. {
    7.     bool playerInZone;
    8.     bool zombieUp;
    9.     bool zombieDown;
    10.     Transform lookAt;
    11.     Animator animator;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         animator = GetComponent<Animator>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if (zombieUp)
    23.         {
    24.             animator.SetBool("triggerzombie", true);
    25.         }
    26.         if (zombieDown)
    27.         {
    28.             animator.SetBool("triggerzombie", false);
    29.         }
    30.         if (playerInZone)
    31.         {
    32.             gameObject.transform.LookAt(lookAt.gameObject.transform);
    33.         }
    34.     }
    35.  
    36.     void RaiseZombie()
    37.     {
    38.         zombieUp = true;
    39.         zombieDown = false;
    40.     }
    41.  
    42.     void LowerZombie()
    43.     {
    44.         zombieDown = true;
    45.         zombieUp = false;
    46.     }
    47.  
    48.     private void OnTriggerEnter(Collider other)
    49.     {
    50.         if (other.CompareTag("Player"))
    51.         {
    52.             RaiseZombie();
    53.             Debug.Log("Zombie found");
    54.         }
    55.     }
    56.  
    57.     private void OnTriggerStay(Collider other)
    58.     {
    59.         if (other.CompareTag("Player"))
    60.         {
    61.             playerInZone = true;
    62.             lookAt = other.gameObject.transform;
    63.         }
    64.     }
    65.  
    66.     private void OnTriggerExit(Collider other)
    67.     {
    68.         if (other.CompareTag("Player"))
    69.         {
    70.             playerInZone = false;
    71.         }
    72.     }
    73. }
     
    Last edited: Nov 28, 2019
  4. thenewkgb82

    thenewkgb82

    Joined:
    Nov 10, 2018
    Posts:
    52
    In fact the second zombie prefab is being placed in the same place as the first. This might actually be because I've keyframed the animation and it's remembered the same place in world coordinates!

    I see I can animate a child gameobject to animate in local coordinates. At the moment my zombies are WAY in the sky even though I've reset all the values to zero. I think this might mean I have to start again.
     
    Last edited: Nov 29, 2019
  5. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    589
    Yes well animation has to be in local space in order for you to move it it around, as it the case usually with character animations etc. It might be better idea to create animation some 3D app like Blender.