Search Unity

Question Why won't the text show up?

Discussion in 'Scripting' started by BetrayedPickle, Jun 30, 2020.

  1. BetrayedPickle

    BetrayedPickle

    Joined:
    Mar 30, 2019
    Posts:
    119
    I am writing a script where a raycast shoots out and if it hits an object, I need this text to show up. Anyone know why it won't?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class CureMakerScript : MonoBehaviour
    7. {
    8.  
    9.     [SerializeField] private string CreateCure = "CreateCureText";
    10.     public Text text;
    11.    
    12.     void Start()
    13.     {
    14.         text.enabled = false;
    15.     }
    16.  
    17.  
    18.     void Update()
    19.     {
    20.  
    21.  
    22.         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    23.         RaycastHit hit;
    24.         if (Physics.Raycast(ray, out hit, 4) && PickUp1.IngredientCollected1 == true && PickUp2.IngredientCollected2 == true && PickUp3.IngredientCollected3 == true)
    25.         {
    26.             var selection = hit.transform;
    27.             if(selection.CompareTag (CreateCure))
    28.             {
    29.                 text.enabled = true;
    30.             }
    31.            
    32.         }
    33.  
    34.     }
    35.  
    36.  
    37. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    What debugging steps have you tried?

    Have you tried logging something inside the if statement on line 24 to see if you're getting in there?
    Have you tried logging something inside the if statement on line 27 to see if you're getting in there?
    Have you tried logging the values of all of your "PickupN" variables?
    Have you checked if the Text component is getting enabled in its inspector?
     
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Like Praetor says above... you are saying "I count 1, 2, 3 and I didn't finish."

    Well, which one didn't finish???

    The obvious followup is check #1 (raycast) because if that fails, #2 (hit object) and #3 (set text) don't matter.

    ETc. etc. Basic engineering. Break the problem down until it is one single line that surprises you.
     
    PraetorBlue likes this.