Проблемы со ссылками на свет в Unity - PullRequest
0 голосов
/ 11 октября 2018

Я пытаюсь сделать игру вождения в Unity, и как часть этого, я пытаюсь заставить светофор мигать и выключаться.

Это мой сценарий, к которому я прикрепилсветофор.Иерархия светофора:

TrafficLight_A (базовый светофор)

  • RedLight (свет, который я пытаюсь сделать вспышкой)
    using UnityEngine;
 using System.Collections;

 public class Blink_Light : MonoBehaviour
 {

     public float totalSeconds = 2;     // The total of seconds the flash wil last
     public float maxIntensity = 8;     // The maximum intensity the flash will reach
     public Light myLight = Light RedLight;        // The light (error)

     public IEnumerator flashNow ()
     {
         float waitTime = totalSeconds / 2;                        
         // Get half of the seconds (One half to get brighter and one to get darker)
         while (myLight.intensity < maxIntensity) {
             myLight.intensity += Time.deltaTime / waitTime;        // Increase intensity
             yield return null;
         }
         while (myLight.intensity > 0) {
             myLight.intensity -= Time.deltaTime / waitTime;        //Decrease intensity
            yield return null;
         }
         yield return null;
     }
 }

Однако я получаю сообщение об ошибке:

Assets/Blink_Script/Blink_Light.cs: error CS1525: Unexpected symbol "RedLight"

Что я могу сделать, чтобы это исправить?(Я немного новичок в C#)

1 Ответ

0 голосов
/ 11 октября 2018

Согласно вашей иерархии, вы пытаетесь получить доступ к компоненту Light из GameObject с именем «RedLight», который является дочерним по отношению к GameObject «TrafficLight_A».Для этого используйте передачу «TrafficLight_A / RedLight» в функцию GameObject.Find, которая находит RedLight GameObject, а затем GetComponent<Light>() для извлечения компонента Light.Вы можете сделать это в функции Awake или Start.

Когда вам нужно найти дочерний объект, "/" используется так же, как и в пути к файлу.

public float totalSeconds = 2;     // The total of seconds the flash wil last
public float maxIntensity = 8;     // The maximum intensity the flash will reach
public Light myLight;

void Awake()
{
    //Find the RedLight
    GameObject redlight = GameObject.Find("TrafficLight_A/RedLight");
    //Get the Light component attached to it
    myLight = redlight.GetComponent<Light>();
}

public IEnumerator flashNow()
{
    float waitTime = totalSeconds / 2;
    // Get half of the seconds (One half to get brighter and one to get darker)
    while (myLight.intensity < maxIntensity)
    {
        myLight.intensity += Time.deltaTime / waitTime;        // Increase intensity
        yield return null;
    }
    while (myLight.intensity > 0)
    {
        myLight.intensity -= Time.deltaTime / waitTime;        //Decrease intensity
        yield return null;
    }
    yield return null;
}
...