【问题标题】:can't add gameobject to a script无法将游戏对象添加到脚本
【发布时间】:2020-09-03 03:10:26
【问题描述】:

我为我的关卡菜单下载了一个脚本,但是当我尝试该脚本时,我无法将我的滚动条分配给该脚本。我试图复制脚本并再次粘贴并制作一个新脚本,但它不起作用。我该如何解决?

using UnityEngine;
using UnityEngine.UI;

public class swipe_menu : MonoBehaviour
{
    public GameObject scrollbar;
    private float scrollpos = 0;
    float[] pos;
    void Start()
    {

    }
    void Update()
    {
        pos = new float[transform.childCount];
        float distance = 1f / (pos.Length - 1f);
        for (int i = 0; i < pos.Length; i++)
        {
            pos[i] = distance * i;
        }

        if (Input.GetMouseButton(0))
        {
            scrollpos = scrollbar.GetComponent<Scrollbar>().value;
        }
        else
        {
            for (int i = 0; i < pos.Length; i++)
            {
                if (scrollpos < pos[i] + (distance / 2) && scrollpos > pos[i] - (distance / 2))
                {
                    scrollbar.GetComponent<Scrollbar>().value = Mathf.Lerp(scrollbar.GetComponent<Scrollbar>().value, pos[i], 0.1f);
                }
            }
        }


        for (int i = 0; i < pos.Length; i++)
        {
            if (scrollpos < pos[i] + (distance / 2) && scrollpos > pos[i] - (distance / 2))
            {
                Debug.LogWarning("Current Selected Level" + i);
                transform.GetChild(i).localScale = Vector2.Lerp(transform.GetChild(i).localScale, new Vector2(1.2f, 1.2f), 0.1f);
                for (int j = 0; j < pos.Length; j++)
                {
                    if (j != i)
                    {
                        transform.GetChild(j).localScale = Vector2.Lerp(transform.GetChild(j).localScale, new Vector2(0.8f, 0.8f), 0.1f);
                    }
                }
            }
        }

    }

【问题讨论】:

  • 您在编辑器中有任何编译错误吗?查看控制台是否有任何无法清除的红色(这是我的猜测)。如果不确定,在尝试添加组件之前等待 unity 编译,如果仍然没有显示,最后关闭并重新启动 unity。

标签: c# unity3d user-interface 3d


【解决方案1】:

您将滚动条用作 GameObject 而不是 Scrollbar,尝试使用 is 就像使用正确的变量一样,它会起作用。

没有很多地方可以找到如何使用滚动条,我能找到的最好的是 https://forum.unity.com/threads/access-scrollbar-value-not-possible-through-code.283486/

【讨论】:

  • 其实我试过了,但是当我再次尝试时它起作用了。谢谢你的帮助。但是有一个问题我的按钮图像消失了。
  • @Mohamad 你想给我发送更多信息(也许是 Git 链接),以便我更好地查看吗?
  • 感谢帮助我,我通过删除脚本并将其再次添加到我的游戏对象来修复它。
猜你喜欢
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多