【问题标题】:ScrollView using Button (NGUI C# UNITY)使用按钮的滚动视图(NGUI C# UNITY)
【发布时间】:2018-12-04 23:41:33
【问题描述】:

我在实现按钮以将其滑动到使用 NGUI 的下一页时遇到问题

这是我的脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NGUI_PageNavigation : MonoBehaviour {

public GameObject sv;
public SpringPanel sp = null;
int xSpring = 1278;
// Use this for initialization
void Start()
{
    sp = sv.GetComponent<SpringPanel>();
    if (sp == null) sv.AddComponent<SpringPanel>();

}

private void OnStoppedMoving()
{

    int pagewidth = 320;
    int pageposition = (int)sp.target.x;
    int page = System.Math.Abs(pageposition / pagewidth) + 1;

    print("page " + (page));
}


public void LeftArrow()
{

    sp.target.x = 1278;
    sp.target.y = 0;
    sp.target.z = 0;
    sp.target = new Vector3(sp.target.x, sp.target.y, sp.target.z);
    sp.enabled = true;

    //Debug.Log("I've been clicked - Left Arrow()");
}

public void RightArrow()
{
    sp.target.x = -1278;
    sp.target.y = 0;
    sp.target.z = 0;
    sp.target = new Vector3(sp.target.x,sp.target.y,sp.target.z);
    sp.enabled = true;
    //Debug.Log("I've been cliked - Right Arrow()");
}
}

它说

NullReferenceException:在需要对象实例的地方发现空值。

谁能帮帮我

【问题讨论】:

    标签: c# unity3d scrollview ngui


    【解决方案1】:

    看看你的这部分代码:

    void Start()
    {
        sp = sv.GetComponent<SpringPanel>();
        if (sp == null) sv.AddComponent<SpringPanel>();
    }
    

    如果GetComponent返回null,因为SpringPanel没有附加到sv游戏对象,SpringPanel组件将被添加到sv游戏对象。问题是sp 仍然是null。您还应该将AddComponent 返回的值分配给sp

    替换

    if (sp == null) 
        sv.AddComponent<SpringPanel>();
    

    if (sp == null) 
        sp = sv.AddComponent<SpringPanel>();
    

    【讨论】:

    • 谢谢。我真是太愚蠢了。非常抱歉
    • 不傻。我们都学习!没问题。你可以接受时可以接受
    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 2019-07-20
    • 2012-03-10
    • 1970-01-01
    • 2022-10-18
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多