Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Cloud956 · Feb 11, 2016 at 08:06 PM · c#2dgameobject2d game2d-platformer

KillPlayer, Null Reference Exception.

Hey there. I've been following a tutorial on how to make a basic platformer. I have all the right scripts, but it simply wont work. Here are the scirpts that I tested o my friends game, and suprisingly there it worked.`using UnityEngine; using System.Collections;

public class LevelManager : MonoBehaviour {

 public GameObject currentCheckpoint;

 private PlayerController player;

 // Use this for initialization
 void Start () {
     player = FindObjectOfType<PlayerController>();

 }

 // Update is called once per frame
 void Update () {

 }

 public void RespawnPlayer()
 {
     Debug.Log ("Player Respawns");
     player.transform.position = currentCheckpoint.transform.position;

}

using UnityEngine; using System.Collections;

public class KillPlayer : MonoBehaviour {

 public LevelManager levelManager;

 // Use this for initialization
 void Start () {
     levelManager = FindObjectOfType<LevelManager>();

 }

 // Update is called once per frame
 void Update () {

 }

 void OnTriggerEnter2D(Collider2D other)
 {
     if(other.name == "Bohater_0")
     {
         levelManager.RespawnPlayer();
     }
 }

} My scene is basically just player, some platform and spikes. The script are linked to the right object, Kill player to spikes, Level Manager to a Level Manager, and i know that i works, because i get the message "Player Respawns". However the transform position doesnt work, and i get the error. I also set the current checkpoint in the Level Manager. Anyone know how to fix this? If you do then please help me!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Zoogyburger · Feb 11, 2016 at 09:33 PM

You need to make sure that your player is named exactly "Bohater_0". When you use OnTriggerEnter2D both the Player and the spikes need to have a Rigidbody2D. if this doesn't solve the problem post your PlayerController script and tell me the exact error.

P.S. If you're new I suggest reading the FAQ and user guide:)

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cloud956 · Feb 11, 2016 at 09:57 PM 0
Share

The name is Bohater_0, and spikes disappeared after adding Rigidbody 2D. What now?

avatar image Cloud956 · Feb 11, 2016 at 09:58 PM 0
Share

ALso i still have a error with The referenced script on this Behaviour ( Game Object "Bohater_0") is missing, but PlayerController does work.

avatar image Cloud956 · Feb 11, 2016 at 10:00 PM 0
Share

Oh, on box collider he had Is trigger on cus it work work without but, and after turning it off, the spikes appeared, and script not working!

avatar image Cloud956 · Feb 11, 2016 at 10:03 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : $$anonymous$$onoBehaviour {
 
     private Rigidbody2D m_Rigidbody;
 
     public float m_Speed ;
     public float m_JumpHeight ;
     public Transform groundCheck;
     public float groundCheckRadius;
     public Layer$$anonymous$$ask whatIsGround;
     private bool grounded;
     private bool  doubleJumped;
     // Use this for initialization
     void Start () {
 
         m_Rigidbody = GetComponent<Rigidbody2D>();
 
     }
     void FixedUpdate(){
         grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
 
     }
 
     // Update is called once per frame
     void Update () {
         if (grounded)
             doubleJumped = false;
 
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)&& grounded) {
             m_Rigidbody.velocity = new Vector2 (m_Rigidbody.velocity.x, m_JumpHeight);
         }
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) && !doubleJumped && !grounded) {
 
             m_Rigidbody.velocity = new Vector2 (m_Rigidbody.velocity.x, m_JumpHeight);
             doubleJumped = true;
 
         }
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.D)) {
 
             m_Rigidbody.velocity = new Vector2 (m_Speed, m_Rigidbody.velocity.y);
 
 
         }
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A)) {
 
             m_Rigidbody.velocity = new Vector2 (-m_Speed, m_Rigidbody.velocity.y);
 
 
         }
 
     }
 
 
 
 }



avatar image Zoogyburger · Feb 12, 2016 at 12:37 AM 1
Share

I made your game and did not modify any script and it works for me. $$anonymous$$aybe you should start over.

  1. I created an empty Gameobject and called it Bohater_0 and gave him the following components: Sprite Renderer, Player Controller(Script), Rigidbody2D, BoxColider2D

  2. I created an empty Gameobject and called it Spikes and gave it the following components: Sprite Renderer, $$anonymous$$ill Player(Script), Rigidbody2D,BoxColider2D

  3. I created an empty Gameobject and called it Level$$anonymous$$anager and gave it the Level $$anonymous$$anager(Script)

  4. I created an empty Gameobject and called it Checkpoint and gave it a Sprite Renderer

  5. I created an empty Gameobject and called it Ground.

  6. On Bohater_0, I set Speed to be 5, Jump Height to be 5, dragged Ground into the Ground Check, Ground Check Radius to be 1, and What is Ground to be Nothing. In the Rigidbody Gravity Scale to be zero, Box Collider to be IsTrigger

  7. On Spikes, dragged my Level$$anonymous$$anager gameobject on to the Level $$anonymous$$anager slot in the $$anonymous$$illPlayer Script. In the Rigidbody Gravity Scale to be zero, Box Collider to be IsTrigger

  8. On Level$$anonymous$$anager, dragged my Checkpoint onto Current Checkpoint If this STILL doesn't work, you're file is probably corrupted.

Show more comments
avatar image
0

Answer by Cloud956 · Feb 11, 2016 at 08:07 PM

Sry for bad script copying, im new here.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

98 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Teleport 2D Character on TriggerEnter 2 Answers

Cannot delay player respawn. 2 Answers

Trouble with directing launched projectiles in Unity 2D 0 Answers

Trouble with controlling the direction of projectiles with C# code 1 Answer

Detect whether a Rigidbody collided with a Trigger 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges