Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Using MoveTowards with prefabs gives different outcome than using them with the objects in hierarchy

Discussion in 'General Discussion' started by Moholbi, Jul 29, 2022.

  1. Moholbi

    Moholbi

    Joined:
    Mar 25, 2022
    Posts:
    2
    Sorry for the bad wording of the problem. Let me explain in a clearer way.

    This is the script that makes the enemy chase the player (vampire survivors clone (of course))
    upload_2022-7-29_15-31-8.png

    When I drag and drop the "Player" in hierarchy to the "Object To Follow", it works as intended however if I drag and drop the "Player" prefab, the enemy just goes to the middle of the plane and stops right there.
    upload_2022-7-29_15-33-19.png

    I'm new to this (started just a month ago) so sorry for the lack of information for you to figure out what the problem is. I can provide more if you tell me what is missing to find out the problem.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,004
    The player prefab is not the same as the player in the scene.

    Scene prefabs are just copies of their respective prefabs in your project assets, so referencing one is not the same as referencing the other.
     
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    Prefab is a file in your project and not an object in your scene. Objects within prefab do not exist in your scene.

    So when you try to follow the prefab, your script will try to follow coordinates stored within prefab. Prefabs are often placed at zero coordinates, that's why your script goes to the middle of the plane.
     
    spiney199 likes this.
  4. Moholbi

    Moholbi

    Joined:
    Mar 25, 2022
    Posts:
    2
    Then how can I make enemies follow the prefab?
    It is not an issue in this case since the player always exists on hierarchy but when I start working with enemy spawners I need to use the enemy prefab because there is not always an enemy in hierarchy.
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,004
    You don't want them to follow the prefab, you want them to follow the instance that exists in the scene. Thus, you want to give the enemies some way to access the position of the player instance in the scene.

    There's a few ways to achieve. The most basic and common would probably be a singleton.
     
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    You can't follow a prefab, because prefab is not an actual object in the scene. It is a template that you use to create objects.

    Spawn your prefab and follow the spawned object instead.
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,592
    General forum is not support forum.
    Please create thread in relevant sub forum. In this case will be Getting Started, or Scripting.