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 Make a GameObject Follow a Path

Discussion in 'Scripting' started by Jakethesnake01, Apr 1, 2023.

  1. Jakethesnake01

    Jakethesnake01

    Joined:
    Jan 25, 2023
    Posts:
    35
    Hello. I am trying to make a prefab follow a path. I have my prefab but Unity won't let me assign the point positions in the inspector. It will only let me choose the positions when a prefab is in the hierarchy. How can I make this script work with my prefab? Thanks.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PoisonPath : MonoBehaviour
    6. {
    7.     [SerializeField] Transform[] Points;
    8.     [SerializeField] private float moveSpeed;
    9.     private int pointsIndex;
    10.  
    11.     void Start()
    12.     {
    13.         transform.position = Points[pointsIndex].transform.position;
    14.     }
    15.  
    16.    
    17.     void Update()
    18.     {
    19.         if(pointsIndex <= Points.Length - 1)
    20.         {
    21.             transform.position = Vector2.MoveTowards(transform.position, Points[pointsIndex].transform.position, moveSpeed * Time.deltaTime);
    22.  
    23.             if(transform.position == Points[pointsIndex].transform.position)
    24.             {
    25.                 pointsIndex += 1;
    26.             }
    27.         }
    28.     }
    29. }
    30.  
     
  2. casezack177

    casezack177

    Joined:
    Aug 10, 2021
    Posts:
    6
    When instantiating a prefab the newly created object is unable to remember any serialized variables assigned to it before instantiation. You will need to find the waypoints for the prefab after it is instantiated.

    One way I was able to do this was to create an empty game object called waypoint container and give it its own tag. Put all of your waypoints into this container. You can then find that container by tag and get its children storing them into a list. Once you have that list you can convert it into an array and this will give you the points you need for your prefab to move across your game space.

    Code (CSharp):
    1. private void FindMapWaypoints()
    2.     {
    3.         GameObject waypointContainer = GameObject.FindGameObjectWithTag("WaypointContainer");
    4.         List<Transform> waypointList = new List<Transform>();
    5.         foreach(Transform child in waypointContainer.transform)
    6.         {
    7.             waypointList.Add(child);
    8.         }
    9.         waypoints = waypointList.ToArray();
    10.      
    11.     }
    I'm sure there are better ways to do this but this one has worked for me so far. I hope it works out for you!
     
  3. Jakethesnake01

    Jakethesnake01

    Joined:
    Jan 25, 2023
    Posts:
    35
    Thanks for the reply. The console is saying "waypoints" doesn't exist in the current context.
     
  4. casezack177

    casezack177

    Joined:
    Aug 10, 2021
    Posts:
    6
    waypoints is the name for the Transform array in the code that I used. You would need to change that to the name of your Transform array or conversely change the name of your Transform array to waypoints.