- Home /
Problem with spawning with companion AI,Script not working properly when GameObject reactivated?
So I recently purchased the Invector melee combat controller which includes a simple AI companion/enemy component. I'm making these companions and I had wanted to have a cutscene that would introduce them.
I'm new to unity and probably doing this improperly however, using cinemachine, I use their prefabs in the cutscene for the animations and then despawn them afterwards. In the same area I have the AI but I have them set to inactive until a trigger is hit.
Essentially the trigger despawns the cutscene prefabs and sets the AI models to active. This all technically works but on reactivating, the AI doesn't work and they just run in circles. I can tell there is an issue with the script loading in because if I just leave them active the entire time they attach when within range. I've tried enabling the scripts after I set the models to active but that doesn't seem to work either. I also attempted to move them from a different spot on the map to the player location on trigger but that would also lead to the unwanted behavior.
The code I use to trigger is really simple but it's right below. Any help would be greatly appreciated.
using UnityEngine;
public class SpawnActor : MonoBehaviour{
// If disabled is set to true then the item will be hidden
public GameObject model;
public bool disabled;
public TriggerType triggerType;
public enum TriggerType{
Once, Everytime,}
protected bool isTriggered;
void Update(){
if (disabled){
objectToSee.SetActive(false);}
else{
objectToSee.SetActive(true);}
}
void OnTriggerEnter(Collider other){
if (triggerType == TriggerType.Once && isTriggered){
return;}
if (other.CompareTag("Player")){
disabled = !disabled;
isTriggered = true;}
}
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Enabling multiple Monobehaviour Components in a game object 1 Answer
Trigger not working 2D 1 Answer
How can you create a trigger collider between two moving objects? 0 Answers