Search Unity

Head-scratcher: duplicated game object acting differently from the original?

Discussion in 'Getting Started' started by AidanofVT, Aug 30, 2020.

  1. AidanofVT

    AidanofVT

    Joined:
    Nov 10, 2019
    Posts:
    104
    Hi all. I can provide details if needed, but there are a lot of details behind this so I'll try to keep it brief for now:

    I am trying to implement 2d pathfinding using The A* Project. Right now, my task is to make one moving object follow another moving object. There were problems that emerged in testing. That's expected, but I realized that the problems only happened when I commanded game object B to follow game object A. When I commanded A to follow B, there were not problems at all! I scrutinized the two game objects, but as far as I could tell they were set up exactly the same. Finally, I tried deleting game object B and just duplicating game object A. Guess what? The duplicate had those same problems! And the original A kept working just fine! I did try moving around their starting locations and varying the order in which they were commanded.

    So: what could cause a duplicate object to behave differently from the original?
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    There could be a lot of reasons, so you'll probably have to share more details and/or screenshots.

    Right off the bat, I'm thinking your "original" object was added in the editor, no? There's a good chance you customized some properties in the instance that aren't matching with what's saved in the prefab you're instantiating.

    Try selecting the object in the scene, going to the Overrides dropdown in the Inspector (near the top right), and selecting "Apply All". See if that changes anything.
     
  3. AidanofVT

    AidanofVT

    Joined:
    Nov 10, 2019
    Posts:
    104
    @Schneider21

    I can't find that Overrides dropdown. The top of my inspector looks like this when I have the problem object selected. Here's the script that controls the path creation and movement:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Pathfinding;
    5.  
    6. public class AidansMovementScript : MonoBehaviour {
    7.     Seeker seeker;
    8.     ABPath path = null;
    9.     Transform transToFollow = null;
    10.     public float speed = 2;
    11.     public float changePointThreshhold = 0.5f;
    12.     public float roundToArrived = 0.5f;
    13.     int currentWaypoint = 0;
    14.  
    15.     void Start() {
    16.         seeker = GetComponent<Seeker>();
    17.     }
    18.  
    19.     public void setDestination (Vector3 destination, Transform movingTransform = null) {
    20.         transToFollow = movingTransform;
    21.         //this IF is needed because the click handler defaults to a destination of 0,0,0 if it doesn't recognise a unit or the ground.
    22.         if (destination != new Vector3(0,0,0)) {
    23.             seeker.StartPath(transform.position, destination, OnPathComplete);
    24.             currentWaypoint = 0;
    25.         }
    26.     }
    27.  
    28.     void OnPathComplete (Path finishedPath) {
    29.         path = (ABPath) finishedPath;
    30.     }
    31.  
    32.     void Update() {
    33.         if (path == null) {
    34.             return;
    35.         }
    36.         if (transToFollow != null && transToFollow.hasChanged == true) {
    37.             Debug.Log("Recalculating.");
    38.             setDestination(transToFollow.position, transToFollow);
    39.             currentWaypoint = 0;
    40.         }
    41.         if (Vector2.Distance(transform.position, path.endPoint) < roundToArrived) {
    42.             terminatePathfinding();
    43.             return;
    44.         }
    45.         //if you are within a specified range of the next waypoint
    46.             if (Vector2.Distance(transform.position, path.vectorPath[currentWaypoint]) < changePointThreshhold) {
    47.                 //and if the number of the next waypoint would not exceeed the number of waypoints in the path
    48.                 if (currentWaypoint + 1 < path.vectorPath.Count - 1) {
    49.                     //increment the currentWaypoint (I think there should be another break here, but it's not in the example)
    50.                     currentWaypoint++;
    51.                 }
    52.                 else {
    53.                     //end reached
    54.                     terminatePathfinding();
    55.                     return;
    56.                 }
    57.             }
    58.         Vector3 dirNew = (path.vectorPath[currentWaypoint] - transform.position).normalized;
    59.         transform.position += dirNew * speed * Time.deltaTime;
    60.         gameObject.transform.hasChanged = true;
    61.     }
    62.  
    63.     void terminatePathfinding () {
    64.         path = null;
    65.         currentWaypoint = 0;
    66.         transToFollow = null;
    67.         gameObject.transform.hasChanged = false;
    68.         Debug.Log("Destination reached.");
    69.     }
    70. }
     
  4. AidanofVT

    AidanofVT

    Joined:
    Nov 10, 2019
    Posts:
    104
    @Schneider21 What are some examples of these sorts of properties that can be specified in the editor but aren't considered part of the prefab? (Also, as far as I know I'm not copying a prefab. I'm copying something within the scene.)
     
  5. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    I don't see anything in this script that's duplicating anything. Could you share that code? Might help narrow down where the problem is happening.
     
  6. AidanofVT

    AidanofVT

    Joined:
    Nov 10, 2019
    Posts:
    104
    Sorry for the confusion: I mean that I'm just right clicking the thing in the editor and clicking "Duplicate."