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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

If statement problem

Discussion in 'Scripting' started by SuloSounds, Feb 19, 2020.

  1. SuloSounds

    SuloSounds

    Joined:
    Feb 13, 2020
    Posts:
    9
    Hi! First of all I want to say that I am an absolute beginner at C#.

    I was following this Cujo Sounds tutorial


    But for some reason at around 13:30 when he is writing the if statements

    Even though I’ve followed the tutorial as accurately as I thought I could, for some reason when I make and if statement under the void update in visual studio the ”if” becomes pink? If I try to make if statements in some other place, it is blue.

    I have no idea how to solve this problem and I’m super grateful if someone could help me with this?
     
  2. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,010
    If you are not just advertising youtube channel than paste here your whole Update method.
     
  3. SuloSounds

    SuloSounds

    Joined:
    Feb 13, 2020
    Posts:
    9
    Here's the whole script. the if statement here in the end is blue but in my visual studio it is pink?? What does it indicate if the word "if" is pink?

    I at least thought I did everything the same way as in the video but I guess I'm just stupid.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(BoxCollider))]
    6. public class AudioPosition : MonoBehaviour
    7. {
    8.  
    9.     private AudioSource AudioEmitter;
    10.     private Vector3 EmitterPosition;
    11.     private GameObject Character;
    12.     private AudioListener Listener;
    13.     private BoxCollider AmbientCollider;
    14.  
    15.     private Vector3 ListenerPosition;
    16.  
    17.     private Vector3 ColliderSizeMax;
    18.     private Vector3 ColliderSizeMin;
    19.  
    20.     private float EmitterX;
    21.     private float EmitterY;
    22.     private float EmitterZ;
    23.  
    24.     private bool IsInArea = false;
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.         AudioEmitter = GetComponentInChildren<AudioSource>();
    30.         EmitterPosition = AudioEmitter.transform.position;
    31.         Character = GameObject.Find("3rdPersonContoller");
    32.         Listener = Character.GetComponent<AudioListener>();
    33.         AmbientCollider = GetComponent<BoxCollider>();
    34.  
    35.         ColliderSizeMax = AmbientCollider.transform.position;
    36.         ColliderSizeMin = AmbientCollider.transform.position;
    37.  
    38.         ColliderSizeMax += (transform.localScale / 2);
    39.         ColliderSizeMin -= (transform.localScale / 2);
    40.  
    41.     }
    42.  
    43.     //Update is called once per frame
    44.     void Update () {
    45.  
    46.         if (ListenerPosition.x > ColliderSizeMin.x && ListenerPosition.x < ColliderSizeMax.x)
    47.         {
    48.             EmitterX = ListenerPosition.x;
    49.         }
    50.  
    51.  
    52.     }
    53.  
    54. }
     
    Last edited: Feb 19, 2020
  4. Kipey1

    Kipey1

    Joined:
    May 26, 2019
    Posts:
    12
    ListenerPosition is not set (is null), maybe that's why?
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I don't believe a vector3 can ever be null. It should initialize with default values (0f,0f,0f).
     
    ZO5KmUG6R and StarManta like this.
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Is your entire question about the color of the keyword in the code editor? Or is there an actual compiler error in your Unity console window, and you just noticed the different color as a symptom of the problem? If there is a compiler error, paste that here, as it will be far more informative than simply, "the word is pink".
     
    Joe-Censored likes this.
  7. SuloSounds

    SuloSounds

    Joined:
    Feb 13, 2020
    Posts:
    9
    Well yes, it is about the color too. I'd just like to know what is that indicative of? And above all voids it says 0 references, what might that mean? And yes I do get an error like this:

    NullReferenceException: Object reference not set to an instance of an object
    AudioPosition.Start () (at Assets/AudioPosition.cs:32)

    It doesn't prevent me from playing the game but the emitter doesn't behave the same way as it does in the tutorial video. As I move inside the collider, the emitter just snaps to a different position and stays there. It is not following the character?

    I ignored the red/pink keywords and proceeded to write the script as it is in the video and it didn't help.

    Here's the whole script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(BoxCollider))]
    6.  
    7. public class AudioPosition : MonoBehaviour {
    8.    
    9.     private AudioSource AudioEmitter;
    10.     private Vector3 EmitterPosition;
    11.     private GameObject Character;
    12.     private AudioListener Listener;
    13.     private BoxCollider AmbientCollider;
    14.  
    15.     private Vector3 ListenerPosition;
    16.  
    17.     private float emitterX;
    18.     private float emitterY;
    19.     private float emitterZ;
    20.  
    21.     private bool IsInArea = false;
    22.  
    23.     private Vector3 ColliderSizeMax;
    24.     private Vector3 ColliderSizeMin;
    25.  
    26.  
    27.     // Start is called before the first frame update
    28.     void Start()  {
    29.         AudioEmitter = GetComponentInChildren<AudioSource>();
    30.         EmitterPosition = AudioEmitter.transform.position;
    31.         Character = GameObject.Find("3rdPersonController");
    32.         Listener = Character.GetComponent<AudioListener>();
    33.         AmbientCollider = GetComponent<BoxCollider>();
    34.  
    35.         ColliderSizeMax = AmbientCollider.transform.position;
    36.         ColliderSizeMin = AmbientCollider.transform.position;
    37.  
    38.         ColliderSizeMax += (transform.localScale / 2);
    39.         ColliderSizeMin -= (transform.localScale / 2);
    40.     }
    41.  
    42.     // Update is called once per frame
    43.     void Update()   {
    44.        
    45.         if (ListenerPosition.x > ColliderSizeMin.x && ListenerPosition.x < ColliderSizeMax.x)
    46.         {
    47.             emitterX = ListenerPosition.x;
    48.         }
    49.  
    50.         if (ListenerPosition.x < ColliderSizeMin.x)
    51.         {
    52.             emitterX = ColliderSizeMin.x;
    53.         }
    54.  
    55.         if (ListenerPosition.x > ColliderSizeMax.x)
    56.         {
    57.             emitterX = ColliderSizeMax.x;
    58.         }
    59.        
    60.         if (ListenerPosition.z > ColliderSizeMin.z && ListenerPosition.z < ColliderSizeMax.z)
    61.         {
    62.             emitterZ = ListenerPosition.z;
    63.         }
    64.  
    65.         if (ListenerPosition.z < ColliderSizeMin.z)
    66.         {
    67.             emitterZ = ColliderSizeMin.z;
    68.         }
    69.  
    70.         if (ListenerPosition.z > ColliderSizeMax.z)
    71.         {
    72.             emitterZ = ColliderSizeMax.z;
    73.         }
    74.  
    75.         if (ListenerPosition.y > ColliderSizeMin.y && ListenerPosition.y < ColliderSizeMax.y)
    76.         {
    77.             emitterY = ListenerPosition.y;
    78.         }
    79.  
    80.         if (ListenerPosition.y < ColliderSizeMin.y)
    81.         {
    82.             emitterZ = ColliderSizeMin.y;
    83.         }
    84.  
    85.         if (ListenerPosition.y > ColliderSizeMax.y)
    86.         {
    87.             emitterY = ColliderSizeMax.y;
    88.         }
    89.  
    90.         if (IsInArea)
    91.         {
    92.             AudioEmitter.transform.position = new Vector3(emitterX, emitterY, emitterZ);
    93.  
    94.         }
    95.     }
    96.  
    97.     private void OnTriggerStay(Collider other)
    98.     {
    99.         if (!IsInArea) { IsInArea = true; }
    100.         AudioEmitter.transform.position = ListenerPosition;
    101.     }
    102.  
    103.     private void OnTriggerExit(Collider other)
    104.     {
    105.         IsInArea = false;
    106.     }
    107. }
    108.  
     
  8. Bisoncraft

    Bisoncraft

    Joined:
    Feb 5, 2016
    Posts:
    37
    If your Null Reference Exception is on line 32, it might mean that your Character is a null reference (so Unity wouldn't be able to search for its components, which is what .GetComponent is trying to do).
    Hence, a possibility is that there is no object called "3rdPersonController". Maybe you forgot to name it, or maybe you made a spelling mistake?
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Technically an exception isn't a compiler error, and therefore wouldn't have any relation to the way the code shows up in the code editor. I've got no idea why that keyword would look different.

    That exception will be caused by the Character variable being null, which probably means that the object in the scene doesn't perfectly match the name you've passed to GameObject.Find. (This is one of many, many reasons not to use GameObject.Find, by the way)
     
  10. SuloSounds

    SuloSounds

    Joined:
    Feb 13, 2020
    Posts:
    9
    I did
    Oh man, I'm amateur! Because I didn't use the exact same assets as in the video tutorial, I noticed that the character I had used from standard assets was called "ThirdPersonController" not "3rdPersonController".

    Thanks a lot for clearing that up for me!