- Home /
Object reference not set to an instance of an object - error
Hi I´m new, and trying to solve a NullREference Problem.I searched, but couldnt get it done. It should be easy, i guess, but i just dont know much about c# ...but i can illustrate/animate. The game can be testplayed here (firefox and arrow keys): http://www.bertelot.de/unity/Furball2D_Testarea/Testbuild.html
Its the Prefab "Cloud" that is instantiated when jumping or boosting-down in-air. It works but i get errors (Object reference not set to an instance of an object) in three lines where it says:
cloudanim = GameObject.Find("Cloud").GetComponent(); ... cloudanim.Play("Boosting"); ... cloudanim.Play("Boosting");
So that cloudanim is the problem. I do not reference the instance of "Cloud".
Could you help me? Heres the script attached to the player:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float maxSpeed = 6f;
public float jumpForce = 1000f;
public Transform groundCheck;
public LayerMask whatIsGround;
public float verticalSpeed = 20;
[HideInInspector]
public bool lookingRight = true;
bool doubleJump = false;
public GameObject Boost;
private Animator cloudanim;
public GameObject Cloud;
private Rigidbody2D rb2d;
private Animator anim;
private bool isGrounded = false;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
Cloud = GameObject.Find("Cloud");
cloudanim = GameObject.Find("Cloud").GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Jump") && (isGrounded || !doubleJump))
{
rb2d.AddForce(new Vector2(0,jumpForce));
if (!doubleJump && !isGrounded)
{
doubleJump = true;
Boost = Instantiate(Resources.Load("Prefabs/Cloud"), transform.position, transform.rotation) as GameObject;
cloudanim.Play("Boosting");
}
}
if (Input.GetButtonDown("Vertical") && !isGrounded)
{
rb2d.AddForce(new Vector2(0,-jumpForce));
Boost = Instantiate(Resources.Load("Prefabs/Cloud"), transform.position, transform.rotation) as GameObject;
cloudanim.Play("Boosting");
}
}
void FixedUpdate()
{
if (isGrounded)
doubleJump = false;
float hor = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (hor));
rb2d.velocity = new Vector2 (hor * maxSpeed, rb2d.velocity.y);
isGrounded = Physics2D.OverlapCircle (groundCheck.position, 0.15F, whatIsGround);
anim.SetBool ("IsGrounded", isGrounded);
if ((hor > 0 && !lookingRight)||(hor < 0 && lookingRight))
Flip ();
anim.SetFloat ("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
}
public void Flip()
{
lookingRight = !lookingRight;
Vector3 myScale = transform.localScale;
myScale.x *= -1;
transform.localScale = myScale;
}
}
Your answer
Follow this Question
Related Questions
Unity Asset Store Not Downloading - "Please Wait" 1 Answer
Error:m TypeTree->m Index != -1 && m Overrides->size() 0 Answers
Rethrow an exception and display new exception's message in console? (C#) 1 Answer
I can not get out from under the Error 0 Answers
can someone fix the loop error.. Why this app running only for a moment in my phone ? 0 Answers