1

I'm having a problem implementing the button to swipe it to the next page I am using NGUI

Here's my script

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()");
}
}

it says

NullReferenceException: A null value was found where an object instance was required.

Could someone help me

1 Answer 1

1

Take a look at this part of your code:

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

If GetComponent returns null because SpringPanel is not attached to the sv GameObject, the SpringPanel component will be added to the sv GameObject. The problem is that sp will still be null. You should also assign the value returned by AddComponent to sp.

Replace

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

with

if (sp == null) 
    sp = sv.AddComponent<SpringPanel>();
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you . That was so dumb of me . I'm very sorry
Not dumb. We all learn! No problem. You can accept when you can

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.