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

Script error

Discussion in 'Scripting' started by Paykoman, Jun 26, 2014.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys.

    Im following the tutorial project of Stealth but im getting a erro in LastPlayerSlighting Script

    the program said me that: error CS0103: The name 'Tags' does not exist in current context

    but in tutorial thats the correct name so it shoudn´t get this error.

    Thats the script image error
    http://postimg.org/image/40ffoy49v/

    thx everyone
     
  2. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    1. Is it really necessary to open up two threads just for this?
    2. Next time please post the code right here in code-tags, not a screen
    3. FindGameObjectsWithTag does take a string as parameter. So "yourTag" instead of Tags.yourTag. ( "mainLight" - "siren" - "alarm" )
     
  3. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Sry for that. So if that is the problem why in tutorial work like a charm with same code that i hv? If we are following a tutorial to learn it should work like he said in tutorial right?
     
  4. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    Tags is most probably a class in the tutorial which holds constant strings and he's looking them up right there.
     
  5. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LastPlayerSlighting : MonoBehaviour
    5. {
    6.     public Vector3 position = new Vector3(1000f, 1000f, 1000f);
    7.     public Vector3 resetPosition = new Vector3(1000f, 1000f, 1000f);
    8.     public float lightHighIntensity = 0.25f;
    9.     public float lightLowIntensity = 0f;
    10.     public float fadeSpeed = 7f;
    11.     public float musicFadeSpeed = 1f;
    12.  
    13.     private AlarmLight alarm;
    14.     private Light mainLight;
    15.     private AudioSource panicAudio;
    16.     private AudioSource[] sirens;
    17.  
    18.     void Awake()
    19.     {
    20.         alarm = GameObject.FindGameObjectWithTag(alarm).GetComponent < AlarmLight();
    21.         mainLight = GameObject.FindGameObjectWithTag(mainLight).light;
    22.         panicAudio = transform.FindChild ("secondaryMusic").audio;
    23.         GameObject[] sirenGameObjects = GameObject.FindGameObjectsWithTag(siren);
    24.         sirens = new AudioSource[sirenGameObjects.Length];
    25.  
    26.         for(int i = 0; i < sirens.Length; i++)
    27.         {
    28.             sirens[i] = sirenGameObjects[i].audio;
    29.         }
    30.     }
    31.  
    32.     void Update()
    33.     {
    34.         SwitchAlarms();
    35.         MusicFading();
    36.     }
    37.  
    38.     void SwitchAlarms()
    39.     {
    40.         alarm.alarmOn = (position != resetPosition);
    41.  
    42.         float newIntensity;
    43.  
    44.         if(position != resetPosition)
    45.         {
    46.             newIntensity = lightLowIntensity;
    47.         }
    48.         else
    49.         {
    50.             newIntensity = lightHighIntensity;
    51.         }
    52.  
    53.         mainLight.intensity = Mathf.Lerp(mainLight.intensity, newIntensity, fadeSpeed * Time.deltaTime);
    54.  
    55.         for(int i =0; i < sirensLenght; i++)
    56.         {
    57.             if(position != resetPosition && !sirens[i].isPlaying)
    58.             {
    59.                 sirens[i].Play();
    60.             }
    61.             else if(position == resetPosition)
    62.             {
    63.                 sirens[i].Stop();
    64.             }
    65.         }
    66.  
    67.     }
    68.  
    69.     void MusicFading()
    70.     {
    71.         if(position != resetPosition)
    72.         {
    73.             audio.volume = Mathf.Lerp(audio.volume, 0f, musicFadeSpeed * Time.deltaTime);
    74.             panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
    75.         }
    76.         else
    77.         {
    78.             audio.volume = Mathf.Lerp(audio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
    79.             panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0f, musicFadeSpeed * Time.deltaTime);
    80.         }
    81.     }
    82. }

    i still get erros event trying to fiz them.. lines 20/21/23and 55, can u plz help me?
     
  6. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    Its a string, it needs to be set between quotation marks!

    Line 53: for(int i =0; i < sirensLenght; i++)
    You've missed the dot: sirens.Lenght
     
  7. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LastPlayerSlighting : MonoBehaviour
    5. {
    6.     public Vector3 position = new Vector3(1000f, 1000f, 1000f);
    7.     public Vector3 resetPosition = new Vector3(1000f, 1000f, 1000f);
    8.     public float lightHighIntensity = 0.25f;
    9.     public float lightLowIntensity = 0f;
    10.     public float fadeSpeed = 7f;
    11.     public float musicFadeSpeed = 1f;
    12.  
    13.     private AlarmLight alarm;
    14.     private Light mainLight;
    15.     private AudioSource panicAudio;
    16.     private AudioSource[] sirens;
    17.  
    18.     void Awake()
    19.     {
    20.         alarm = GameObject.FindGameObjectWithTag("alarm").GetComponent<AlarmLight();
    21.         mainLight = GameObject.FindGameObjectWithTag("mainLight").light;
    22.         panicAudio = transform.FindChild ("secondaryMusic").audio;
    23.         GameObject[] sirenGameObjects = GameObject.FindGameObjectsWithTag("siren");
    24.         sirens = new AudioSource[sirenGameObjects.Length];
    25.  
    26.         for(int i = 0; i < sirens.Length; i++)
    27.         {
    28.             sirens[i] = sirenGameObjects[i].audio;
    29.         }
    30.     }
    31.  
    32.     void Update()
    33.     {
    34.         SwitchAlarms();
    35.         MusicFading();
    36.     }
    37.  
    38.     void SwitchAlarms()
    39.     {
    40.         alarm.alarmOn = (position != resetPosition);
    41.  
    42.         float newIntensity;
    43.  
    44.         if(position != resetPosition)
    45.         {
    46.             newIntensity = lightLowIntensity;
    47.         }
    48.         else
    49.         {
    50.             newIntensity = lightHighIntensity;
    51.         }
    52.  
    53.         mainLight.intensity = Mathf.Lerp(mainLight.intensity, newIntensity, fadeSpeed * Time.deltaTime);
    54.  
    55.         for(int i =0; i <sirens.Length; i++)
    56.         {
    57.             if(position != resetPosition && !sirens[i].isPlaying)
    58.             {
    59.                 sirens[i].Play();
    60.             }
    61.             else if(position == resetPosition)
    62.             {
    63.                 sirens[i].Stop();
    64.             }
    65.         }
    66.  
    67.     }
    68.  
    69.     void MusicFading()
    70.     {
    71.         if(position != resetPosition)
    72.         {
    73.             audio.volume = Mathf.Lerp(audio.volume, 0f, musicFadeSpeed * Time.deltaTime);
    74.             panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
    75.         }
    76.         else
    77.         {
    78.             audio.volume = Mathf.Lerp(audio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
    79.             panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0f, musicFadeSpeed * Time.deltaTime);
    80.         }
    81.     }
    82. }
    evrything is fixed now.. just getting an error CS 0119 in line 20... And thx again for ur help _O_
     
  8. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    If it gives you an error there might be something wrong at line 20. Maybe you have a typo there. Have you acctually looked after a typo after I've just pointed out four ones to you previously?

    alarm = GameObject.FindGameObjectWithTag("alarm").GetComponent<AlarmLight();

    Whats missing? Maybe a closing angle bracket?
     
  9. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Seriously sorry man but in that line i just see all brackets open and close...

    alarm = GameObject.FindGameObjectWithTag("alarm").GetComponent<AlarmLight();

    i dont see anyone to close... so thx for the help i dont borrow anymore.. has i said im new in C# and i dont get a lot of things yet. Appologizes.
     
  10. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    Try alarm = GameObject.FindGameObjectWithTag("alarm").GetComponent<AlarmLight>();

    Notice the > before the final (); That is what is missing in your code.
     
  11. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    -.- damm so easy and so blind :p thx m8 really appreciate it
     
  12. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    No problem, easy stuff to miss when you are new to coding. :)