Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need help on a newbie error

Discussion in 'Scripting' started by auratha11, Aug 19, 2014.

  1. auratha11

    auratha11

    Joined:
    Aug 19, 2014
    Posts:
    3
    Hello, I just started getting into Unity and tried out my first game.
    It is based on a tutorial by "Sebastian Lague" from Youtube (thanks for a great video).

    This is PlayerPhysics.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerPhysics : MonoBehaviour {
    5. public void Go(Vector2 GoAmount){
    6.                 transform.Translate(GoAmount);
    7.         }
    8.  
    9. }
    10.  
    This is PlayerControl.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(PlayerPhysics))]
    5. public class PlayerControl : MonoBehaviour {
    6.     //Player handling
    7.     public float speed = 8;
    8.     public float acceleration = 12;
    9.  
    10.     private float currentSpeed;
    11.     private float targetSpeed;
    12.     private Vector2 amountToMove;
    13.  
    14.     private PlayerPhysics playerPhysics;
    15.  
    16.     void Start () {
    17.         playerPhysics = GetComponent<PlayerPhysics>();
    18.     }
    19.  
    20.  
    21.     void Update () {
    22.         targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
    23.         currentSpeed = IncrementTowards(currentSpeed, targetSpeed, acceleration);
    24.  
    25.         amountToMove = new Vector2(currentSpeed, 0);
    26.         playerPhysics.Go(amountToMove*Time.deltaTime);
    27.     }
    28.     //increase n towards target by speed
    29.     private float IncrementTowards(float n, float target, float a) {
    30.         if (n==target){
    31.             return n;
    32.         }
    33.         else {
    34.             float dir = Mathf.Sign(target - n); //must n be decreased or increased to get closer to the target
    35.             n+=a*Time.deltaTime*dir;
    36.             return(dir==Mathf.Sign(target-n))? n:target; //if n has now passed target then return target, otherwise return n
    37.         }
    38.     }
    39. }
    40.  
    Error messages shown are:
    NullReferenceException: Object reference not set to an instance of an object
    PlayerControl.Update () (at Assets/scripts/PlayerControl.cs:26)

    Help pls!
     
  2. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    Well it looks like it is not finding the play physics script off of your gameObject. Do me a favor: change playerPhysics to public and start the game again. Then if you look in the inspector you should see a variable called Player Physics. Is there anything in there or is it empty?
     
  3. auratha11

    auratha11

    Joined:
    Aug 19, 2014
    Posts:
    3
    Hey, thanks for the reply! I changed it to public now.
    And it is not empty in inspector. It has codes from PlayerPhysics.cs
     
  4. auratha11

    auratha11

    Joined:
    Aug 19, 2014
    Posts:
    3
    then again, inspector gone empty when I run the game second time. :eek:
     
  5. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    Try adding gameObject. in front of the get component under the start method. I don't think it will help much but it might. Unity gets picky sometimes lol