Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Help changing position of gameObjects if on certain scenes

Discussion in 'Scripting' started by Hydrolios, Dec 7, 2022.

  1. Hydrolios

    Hydrolios

    Joined:
    Dec 19, 2021
    Posts:
    6
    Hello, I am trying to figure out how to move my NPCs away from their original position if not on the scene they are spawned in.

    I am trying to make a dungeon where I have reusable Guards as a prefab, and when players walk in their line of sight, a battle is triggered and after battle, the NPC that just battled them are moved away in the scene, if the player battles every single Guard as they go through each room, my desired outcome is reached as I am using DontDestroyOnLoad for each Guard to keep the data that they have been triggered. The problem is when the player skips 1 or more Guards, because they have DontDestroyOnLoad, they will be seen in the next or previous scene.

    How should I carry this out? Below is a video showcasing what is happening, the first room where you see me encounter 2 guards gives the desired outcome of being able to traverse without worry of seeing the guards, but the second room, I move to another room without fully clearing it so the Guards followed.



    The code for the Guards is this

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Encounter : MonoBehaviour
    7. {
    8.     [SerializeField] Dialogue dialogue;
    9.  
    10.     public GameObject triggered;
    11.     public string sceneToLoad;
    12.     public static bool defeated;
    13.    
    14.     public string objectID;
    15.     private void Awake()
    16.     {
    17.         objectID = name + transform.position.ToString();
    18.     }
    19.    
    20.  
    21.     void Start()
    22.     {
    23.         triggered.SetActive(false); //notification above NPC to display if they have been triggered by player entering line of sight
    24.         for (int i = 0; i < Object.FindObjectsOfType<Encounter>().Length; i++)
    25.         {
    26.             if(Object.FindObjectsOfType<Encounter>()[i] != this)
    27.             {
    28.                 if (Object.FindObjectsOfType<Encounter>()[i].objectID == objectID)
    29.                 {
    30.                     Destroy(gameObject);
    31.                 }
    32.             }
    33.         }
    34.    
    35.         DontDestroyOnLoad(gameObject);
    36.        
    37.     }
    38.     private void Update()
    39.     {
    40.         if (defeated && triggered.activeInHierarchy)
    41.         {
    42.             Defeated(new Vector2(999f, 999f));
    43.         }
    44.     }
    45.     public void OnTriggerEnter2D(Collider2D other)
    46.     {
    47.         if (other.CompareTag("Player") && !other.isTrigger)
    48.         {
    49.             defeated = false;
    50.             triggered.SetActive(true);
    51.             StartCoroutine(DialogueManager.Instance.ShowDialogueV3(dialogue, sceneToLoad, true));
    52.            
    53.            
    54.         }
    55.     }
    56.  
    57.     public void Defeated(Vector2 moveaway)
    58.     {
    59.         transform.position = moveaway;
    60.         triggered.SetActive(false);
    61.         defeated = false;
    62.     }
    63. }
    64.