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. Dismiss Notice

Question Instantiated Prefabs do not move and animate simultaneously

Discussion in 'Scripting' started by GoblinOfFrogs, Jul 13, 2023.

  1. GoblinOfFrogs

    GoblinOfFrogs

    Joined:
    Jun 14, 2023
    Posts:
    5
    I am an absolute novice and have only been working with unity a few weeks.
    I have managed to figure out a good deal by myself though tutorials or experimentation but this particular problem has me stumped.

    Goal:
    NPC's will be instantiated at the screen edge every few seconds and move to the other edge. If the player interacts with one of them by key press, that instance of NPC should play an animation. During this animation, the player sprite will become hidden and player movement will be disabled. On a second key press, the player becomes visible again and movement is re-enabled.

    Currently:
    Instantiated NPC's do not move. They spawn in on the correct timer (random range between 2 - 5) and in the correct place (Empty game object at the left edge of the screen). The original Prefab for the NPC exists in the scene, fully functional in movement. If this original prefab is removed there is no change on the instantiated prefabs. If any NPC is interacted with, the same animation will play for each instance simultaneously. If a new NPC is instantiated while the animation is playing, they will immediately begin to animate out of sync.

    NPC Movement Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ClientMovement : MonoBehaviour
    6. {
    7.     Animator animator;
    8.     public float moveSpeed = 3;
    9.     public Vector3 vector3;
    10.  
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.        
    16.         animator = gameObject.GetComponent<Animator>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         transform.position = transform.position + (Vector3.right * moveSpeed) * Time.deltaTime;
    23.  
    24.         if (animator.GetBool("Talk"))
    25.  
    26.         {
    27.             moveSpeed = 0;
    28.         }
    29.         else
    30.         {
    31.             moveSpeed = 3;
    32.         }
    33.     }
    34. }
    35.  
    NPC Spawn Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ClientBizSpawn : MonoBehaviour
    6. {
    7.     public GameObject ClientBusiness;
    8.     public float spawnRate;
    9.     public float timer;
    10.     ClientMovement clientMovement;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         spawnRate = Random.Range(2, 5);
    15.         SpawnClientBiz();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (timer < spawnRate)
    22.         {
    23.             timer = timer + Time.deltaTime;
    24.         }
    25.         else
    26.         {
    27.             SpawnClientBiz();
    28.             timer = 0;
    29.             spawnRate = Random.Range(2, 5);
    30.         }
    31.     }
    32.  
    33.     void SpawnClientBiz()
    34.     {
    35.  
    36.         Instantiate(ClientBusiness, transform.position, transform.rotation);
    37.  
    38.     }
    39. }
    40.  
    NPC Animation Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.VisualScripting;
    4. using UnityEngine;
    5. using UnityEngine.UIElements;
    6.  
    7. public class TalkTransition : MonoBehaviour
    8. {
    9.     public SpriteRenderer spriteRenderer;
    10.     public Transform talkTransform;
    11.     PlayerMovement playerMoveScript;
    12.     Collision collisionScript;
    13.     Animator animator;
    14.  
    15.     public bool playerInRange;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         spriteRenderer = GameObject.FindGameObjectWithTag("Player").GetComponent<SpriteRenderer>();
    21.         GameObject.FindGameObjectWithTag("Player").GetComponent<Collision>();
    22.         animator = gameObject.GetComponent<Animator>();
    23.         animator.SetBool("Talk", false);
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.        
    30.      
    31.             if (!spriteRenderer.enabled)
    32.             {
    33.                 animator.SetBool("Talk", true);
    34.                
    35.         }
    36.             else
    37.             {
    38.                 animator.SetBool("Talk", false);
    39.                  
    40.         }
    41.  
    42.        
    43.     }
    44.  
    45.  
    46.     private void FixedUpdate()
    47.     {
    48.        
    49.        
    50.     }
    51. }
    52.  
    Any help with the current issue would be greatly appreciated as would any simply worded explanations on why this was wrong and why the answer is correct
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    This isn't related to 2D. I'll move your post to the Scripting forum for you.

    Note that there's also an Animation forum.
     
  3. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    431
    First of all, take a piece of paper and write down a sequence diagram. Second, put Debug.Log into the code so you can compare what you want and what the program is doing.
     
  4. GoblinOfFrogs

    GoblinOfFrogs

    Joined:
    Jun 14, 2023
    Posts:
    5
    Thank you for the suggestion, Debug Logs showed the animations were triggering, which was already evident, but that the movement was not.
    It seems that for whatever reason, the movement script tied to the prefab was not being created on the instantiated objects. Removing and reapplying the script seems to have fixed that particular issue.

    I am still however somewhat stumped on how to correct the issue of simultaneous animations triggering.
     
  5. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    The prefab doesn't have to be in the scene. You can drag the prefab to the script in the editor.

    What does your code look like that triggers the "Talk" mode?