Search Unity

Failure to pass Bool. Help pls

Discussion in 'Scripting' started by Darktower121, Sep 15, 2020.

  1. Darktower121

    Darktower121

    Joined:
    Aug 27, 2020
    Posts:
    5
    New to Unity and Coding in general. Trying to click on an enemy and when I do that change a bool. Enemy script has reference to Manager script where the bool is. Super confused why its not doing anything> Any help would be greatly appreciated. Enemy Value script is trying to access the bool passer on the Conflict Manager script.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyValues : MonoBehaviour
    6. {
    7.     public int enemyHealth = 100;
    8.     public int enemyThingees = 2;
    9.     public GameObject managerRef;
    10.  
    11.     public void Start()
    12.     {
    13.         managerRef.GetComponent<ConflictManager>();
    14.         ConflictManager conflictManager = managerRef.GetComponent<ConflictManager>();
    15.     }
    16.     public void OnMouseDown()
    17.     {
    18.         if(Input.GetMouseButtonDown(0))
    19.         {
    20.             conflictManager.passer = true;
    21.         }
    22.     }
    23. }
    24.  
    Code (CSharp):
    1. public class ConflictManager : MonoBehaviour
    2. {
    3.     public GameObject playerValueRefrence;
    4.     public GameObject enemyValueRefrence;
    5.     public bool passer = false;
    6.  
    7.  
    8.     private void Awake()// passing variable from another script and printing it
    9.     {
    10.         playerValueRefrence.GetComponent<PlayerValues>();
    11.         PlayerValues playerValues = playerValueRefrence.GetComponent<PlayerValues>(); // name after what object is
    12.  
    13.         enemyValueRefrence.GetComponent<EnemyValues>();
    14.         EnemyValues enemyValues = enemyValueRefrence.GetComponent<EnemyValues>();
    15.        
    16.      
    17.    
    18.     }
    19.     public void Update()
    20.     {
    21.         if (passer == true)
    22.         {
    23.             Battle();
    24.         }
    25.     }
    26.  
    27.     void Battle()
    28.     {
    29.         Debug.Log("You gave away a Thingee");
    30.        
    31.     }
    32. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Your EnemyValues script won't compile as written. "conflictManager" is defined as a local variable in Start(), but you're referencing it in OnMouseDown(). That's not legal C#. You would have to make it an instance variable to access it across methods.

    Anyway, there's a simpler way. Instead of using
    public GameObject managerRef;
    and then calling GetComponent, just make it a ConflictManager reference directly:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class EnemyValues : MonoBehaviour
    5. {
    6.     public int enemyHealth = 100;
    7.     public int enemyThingees = 2;
    8.  
    9.     // Assign this value by dragging and dropping in the inspector
    10.     public ConflictManager conflictManager;
    11.  
    12.     public void OnMouseDown()
    13.     {
    14.         if(Input.GetMouseButtonDown(0))
    15.         {
    16.             conflictManager.passer = true;
    17.         }
    18.     }
    19. }
    Then you can drag and drop the ConflictManager reference in the inspector the same way you did for the GameObject and you're good to go.
     
    Darktower121 and Kurt-Dekker like this.
  3. Darktower121

    Darktower121

    Joined:
    Aug 27, 2020
    Posts:
    5
    Thank you for the help.