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

I need help figuring out why my two scripts aren't cooperating with eachother

Discussion in 'Scripting' started by mattiascombitech, Jul 14, 2022.

  1. mattiascombitech

    mattiascombitech

    Joined:
    Jun 15, 2022
    Posts:
    5
    Hey!

    So basically I am making a car game, where I want my AI to follow a certain path. To do this I made one script for the AI and one for the path. However, for some reason the two scripts don't seem to cooperate with eachother, and I don't know why.


    This is the script for my AI:


    Code (CSharp):
    1.     using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.     using UnityEngine.AI;
    5.    
    6.     public class NPC : MonoBehaviour
    7.     {
    8.         public GameObject destinationPoint;
    9.         NavMeshAgent theAgent;  
    10.    
    11.         void Start()  
    12.         {
    13.             theAgent = GetComponent<NavMeshAgent>();
    14.         }
    15.    
    16.         void Update()
    17.         {
    18.             theAgent.SetDestination(destinationPoint.transform.position);
    19.         }
    20.     }
    21.  
    This is the script for the path:


    Code (CSharp):
    1.     using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.     using UnityEngine.AI;
    5.    
    6.     public class NPC3Track : MonoBehaviour
    7.     {
    8.         public GameObject TheMarker;  
    9.         public GameObject Destination1;
    10.         public GameObject Destination2;
    11.         public GameObject Destination3;
    12.         public GameObject Destination4;
    13.         public GameObject Destination5;
    14.         public GameObject Destination6;
    15.         public int MarkTracker;
    16.    
    17.    
    18.         void Update()    
    19.         {
    20.             if (MarkTracker == 0)
    21.             {
    22.                 TheMarker.transform.position = Destination1.transform.position;    
    23.             }
    24.             if (MarkTracker == 1)
    25.             {
    26.                 TheMarker.transform.position = Destination2.transform.position;    
    27.             }
    28.             if (MarkTracker == 2)
    29.             {
    30.                 TheMarker.transform.position = Destination3.transform.position;    
    31.             }
    32.             if (MarkTracker == 3)
    33.             {
    34.                 TheMarker.transform.position = Destination4.transform.position;    
    35.             }
    36.             if (MarkTracker == 4)
    37.             {
    38.                 TheMarker.transform.position = Destination5.transform.position;    
    39.             }
    40.             if (MarkTracker == 5)
    41.             {
    42.                 TheMarker.transform.position = Destination6.transform.position;    
    43.             }
    44.         }
    45.         private IEnumerator OnTriggerEnter(Collider collision)
    46.         {
    47.             if (collision.gameObject.tag == "NPC")
    48.             {
    49.                 this.GetComponent<MeshCollider>().enabled = false;
    50.                 MarkTracker += 1;
    51.                 if (MarkTracker == 6)
    52.                 {
    53.                     MarkTracker = 0;
    54.                 }
    55.             }
    56.             yield return new WaitForSeconds(1);
    57.             this.GetComponent<MeshCollider>().enabled = true;
    58.         }
    59.     }
    60.  
     
  2. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    What do you mean with "cooperate with each other"?

    You don´t call your script NPC in the NPC3TRACK and you also don´t call your NPC3TRACK script in the NPC script. So how should they know from each other ?

    What do you try to achieve ?
     
    Nefisto likes this.
  3. Nefisto

    Nefisto

    Joined:
    Sep 17, 2014
    Posts:
    324
    I think that each of his NPC has both components and that "TheMarker" from NPC3TRACK and "destinationPoint" from NPC is the same GO, and I think that this is what he means by "cooperate"

    If I was right, try to check if the trigger is happening, to trigger both objects must have colliders, exactly one of them must be marked as a trigger and you must have at least one rigid body, also check if the tag for NPC is set correctly.

    With this said, make sure to disable the marker that you passed instead of disabling the NPC collider, this will avoid players from increasing the marker counter passing through old markers.
     
    mattiascombitech likes this.
  4. mattiascombitech

    mattiascombitech

    Joined:
    Jun 15, 2022
    Posts:
    5
    Yes, thank you! The issue was that I had both marked as triggers