【问题标题】:Gameobject ( spotlight ) is not interacting/changing with UI canvas textField游戏对象(聚光灯)不与 UI 画布 textField 交互/更改
【发布时间】:2019-02-12 05:49:22
【问题描述】:

游戏对象(聚光灯)没有响应 UI(文本)

我的意图是使用rest api从firebase中获取值并根据值闪烁灯,问题在于它非常正确地获取值但我无法连接到游戏对象

例如:

在火力基地下 值:75(键值对)

它被正确获取,但根据值它没有改变颜色

using UnityEngine;
using UnityEngine.UI;
using Proyecto26;

public class valuesRetrieve : MonoBehaviour
{
    int l;
    Color color0 = Color.red;
    Color color1 = Color.green;

    Light lt;

    public Text displayName;
    public InputField nameText;
    public static string name;

    public Text valueText;
    public static int store_values;

    User user = new User();

    void Start()
    {
        onSubmit();

        //l = liver(l);
        //lt = GetComponent<Light>();
    }

    public void onSubmit()
    {
        RetrieveFromDatabase();
    }

    private void RetrieveFromDatabase()
    {
        RestClient.Get<User>("https://feelsmart-******.firebaseio.com/" + nameText.text + ".json").Then(response =>
        {
            user = response;

            UpdateValues();
        });
    }

    public void UpdateValues()
    {
        valueText.text = "store value is " + user.StoreValues;

        lt = GetComponent<Light>();
        //user.StoreValues = 75;
        if (user.StoreValues < 72 || user.StoreValues > 80)
        {
            lt.color = color0;
        }
        else
        {
            lt.color = color1;
        }
 }

我希望颜色会改变

【问题讨论】:

    标签: c# visual-studio firebase unity3d


    【解决方案1】:

    你会得到一个MissingComponentException:

    “GameObject”游戏对象没有附加“Light”,但脚本正在尝试访问它。

    lt = GetComponent<Light>();
    

    尝试获取该脚本所附加到的相同 GameObject 的组件Light


    如果您只想更改第一个找到的孩子Light,您必须使用GetComponentInChildren&lt;Type&gt;()

    // GetComponentInChildren by default only finds active and enabled components
    // -> use true if you want to also get components if they are disabled
    // or the GameObject inactive
    light = GetComponentInChildren<Light>();
    
    light.color = (user.StoreValues < 72 || user.StoreValues > 80) ? color0 : color1;
    

    或者如果您想更改该对象下的所有Lights,您必须使用GetComponentsInChildren&lt;Type&gt;()(注意s)并迭代它们

    // GetComponentsInChildren by default only finds active and enabled components
    // -> use true if you want to also get components if they are disabled
    // or the GameObject inactive
    lights = GetComponentsInChildren<Light>(true);
    foreach(var light in lights)
    {
        light.color = (user.StoreValues < 72 || user.StoreValues > 80) ? color0 : color1;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 2015-12-03
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 2023-04-04
      相关资源
      最近更新 更多