Unity-未将对象引用设置为对象的实例

时间:2019-11-25 01:27:13

标签: c# unity3d

我的项目中的两个脚本突然停止工作。但是,我没有对其进行任何更改,所以我不知道是什么原因导致了错误。

这是发生错误的脚本之一。它指定它是由于行“ scoreText.text = wumpaCollect.wumpaCounter.ToString();”

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

public class score : MonoBehaviour
{
    public static Text scoreText;
    void Update()
    {
        scoreText.text = wumpaCollect.wumpaCounter.ToString();
    }
}

这是引用的脚本:

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

public class wumpaCollect : MonoBehaviour
{
    public static int wumpaCounter = 0;
    public AudioSource wumpaCollectSound;
    bool pickedUp = false;
    Animator animator;
    SpriteRenderer spriteRenderer;

    private void Start()
    {
        animator = GetComponent<Animator>();
        spriteRenderer = GetComponent<SpriteRenderer>();
        animator.Play("wumpaSpin");
    }
    void OnTriggerEnter2D(Collider2D col)
    {
        if(pickedUp)
        {
            return;
        }
        if (col.tag == "Player")
        {
            wumpaCollectSound.Play();
            transform.position = Vector2.one * 9999f;
            Destroy(gameObject, wumpaCollectSound.clip.length);
            wumpaCounter++;
            pickedUp = true;
        }

    }
}

我用类似的脚本得到了完全相同的错误:

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

public class lives : MonoBehaviour
{
    public static Text lifeText;
    void Update()
    {
        lifeText.text = PlayerController2D.lives.ToString();
    }
}

这是引用的脚本:

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

public class PlayerController2D : MonoBehaviour
{
    Animator animator;
    Rigidbody2D rigidBody2D;
    SpriteRenderer spriteRenderer;
    bool isGrounded;
    bool canDoubleJump = false;
    public float fallMultiplier = 2.5f;
    public float lowJumpMultiplier = 2f;
    public static int lives = 4;

    [SerializeField]
    Transform groundCheck;
    [SerializeField]
    Transform groundCheckL;
    [SerializeField]
    Transform groundCheckR;

    [SerializeField]
    public float runSpeed = 4f;

    [SerializeField]
    public float jumpSpeed = 4f;

    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
        rigidBody2D = GetComponent<Rigidbody2D>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }
    private void FixedUpdate()
    {
        if(Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"))
            || Physics2D.Linecast(transform.position, groundCheckR.position, 1 << LayerMask.NameToLayer("Ground"))
            || Physics2D.Linecast(transform.position, groundCheckL.position, 1 << LayerMask.NameToLayer("Ground")))
        {
            isGrounded = true;
        }
        else
        {
            isGrounded = false;
        }

        if(Input.GetKey("a") || Input.GetKey("left"))
        {
            rigidBody2D.velocity = new Vector2(-runSpeed, rigidBody2D.velocity.y);

            if(isGrounded)
                animator.Play("crashrun");

            spriteRenderer.flipX = true;
        }
        else if(Input.GetKey("d")|| Input.GetKey("right"))
        {
            rigidBody2D.velocity = new Vector2(runSpeed, rigidBody2D.velocity.y);

            if(isGrounded)
                animator.Play("crashrun");

            spriteRenderer.flipX = false;
        }
        else
        {
            if(isGrounded)
            {
                animator.Play("crashStandBy");
            }


            rigidBody2D.velocity = new Vector2(0, rigidBody2D.velocity.y);
        }

        //caida luego de saltar
        if (rigidBody2D.velocity.y < 0)
        {
            rigidBody2D.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.fixedDeltaTime;
        }
    }

    private void Update()
    {
        if (Input.GetKeyDown("space"))
        {
            if (isGrounded)
            {
                rigidBody2D.velocity = new Vector2(rigidBody2D.velocity.x, jumpSpeed);
                animator.Play("crashJump");
                canDoubleJump = true;
            }
            else if (canDoubleJump)
            {
                canDoubleJump = false;
                rigidBody2D.velocity = new Vector2(rigidBody2D.velocity.x, jumpSpeed);
                animator.Play("crashJump");
            }
        }
    }

}

0 个答案:

没有答案