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 WHY IS THIS CODE NOT WORKING?!?

Discussion in 'Scripting' started by alex_764, Mar 5, 2023.

  1. alex_764

    alex_764

    Joined:
    Nov 24, 2021
    Posts:
    4
    Code (CSharp):
    1. public void OnClick()
    2. {
    3. if (_canPlaceFarm && _selectedTile != null)
    4. {
    5. Instantiate(_tilePrefab.farmPrefab, new Vector2(_highspawn.position.x, _highspawn.position.y), Quaternion.identity);
    6. _gridManager.warningText.SetActive(false);
    7. _canPlaceFarm = false;
    8. _selectedTile._gridManager.SetFarmHasBeenPlaced(true);
    9. }
    10. }
    So this code is supposed to disable the warningText GameObject when OnClick is triggered. But for some reason it just... doesn't? There's nothing that should make it not work, every other bit of the code works fine and there are no errors... SO WHY?!?
     
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    So everything else within that if statement is functioning? If so, are you sure you've referenced the object correctly?
    Try removing everything else and directly referencing a gameobject to get to the root of the problem. Like the following, which works as expected. Then, when you've proven it to work at the most basic of levels, try to figure out why your gameobject isn't behaving the same way.

    Code (CSharp):
    1. public GameObject myGameobject;
    2.  
    3. public void OnClick()
    4. {
    5.     //if (_canPlaceFarm && _selectedTile != null)
    6.     //{
    7.         //Instantiate(_tilePrefab.farmPrefab, new Vector2(_highspawn.position.x, _highspawn.position.y), Quaternion.identity);
    8.         //_gridManager.warningText.SetActive(false);
    9.         myGameobject.SetActive (false);
    10.         //_canPlaceFarm = false;
    11.         //_selectedTile._gridManager.SetFarmHasBeenPlaced(true);
    12.     //}
    13. }
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,893
    A simple Debug.Log will tell if you if the code is running or not. After that, print out the values you expect and see if they match what you think they should be.

    Most likely there is a bug in your logic, or something hasn't been set up correctly.
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
  5. dogmachris

    dogmachris

    Joined:
    Sep 15, 2014
    Posts:
    1,373
    Well, I don't have a crystal ball to see the rest of your code, but I'm guessing that the OnClick() method is about as popular as the last kid picked for dodgeball. It's just sitting there, all alone, waiting for someone to call it, but nobody seems to care.

    In all seriousness though, no offense, but how many people would have to tell you to put a
    Code (CSharp):
    1. Debug.Log("Knock Knock Neo...");
    in there, before you just do it and see what happens?
     
    Homicide likes this.
  6. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    If it does not enter the if condition you can assume that either _canPlaceFarm is false or selectedTile is null, a simple Debug.Log will tell you what happens. Alternatively it may not call OnClick() at all.