Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

can't move the player

Discussion in 'Scripting' started by chipp_zanuff, Feb 4, 2017.

  1. chipp_zanuff

    chipp_zanuff

    Joined:
    Jul 26, 2016
    Posts:
    28
    hi, i'm confused. i'm learning unity from here:



    but why when i click the play button, i can't move my player?

    here's the code:
    Code (CSharp):
    1. // script for player
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. [RequireComponent (typeof (PlayerCtrller))]
    7.  
    8. public class Player_Scrpt : MonoBehaviour {
    9.     public float move_speed = 5;
    10.     PlayerCtrller controller;
    11.  
    12.     void Start () {
    13.         controller = GetComponent<PlayerCtrller>();
    14.     }
    15.  
    16.     void Update () {
    17.         Vector3 move_input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    18.         Vector3 move_velocity = move_input.normalized * move_speed;
    19.         controller.Move(move_velocity);
    20.     }
    21. }
    Code (CSharp):
    1. // script for player controller
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. [RequireComponent (typeof (Rigidbody))]
    7.  
    8. public class PlayerCtrller : MonoBehaviour {
    9.     Rigidbody my_rbody;
    10.     Vector3 velocity;
    11.     void Start () {
    12.         my_rbody = GetComponent<Rigidbody>();
    13.     }
    14.  
    15.     public void Move(Vector3 v)
    16.     {
    17.         velocity = v;
    18.     }
    19.  
    20.     public void FixedUpdate()
    21.     {
    22.         my_rbody.MovePosition(my_rbody.position + velocity * Time.fixedDeltaTime);
    23.     }
    24. }
    the error msg says:
    NullReferenceException: Object reference not set to an instance of an object Player_Scrpt.Update () (at Assets/Scripts/Player_Scrpt.cs:18) unity

    can somebody tell me what's wrong in my script?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,506
    Works for me..
    Does your player have those both scripts attached?
     
    chipp_zanuff likes this.
  3. chipp_zanuff

    chipp_zanuff

    Joined:
    Jul 26, 2016
    Posts:
    28
    gee... i don't know it should be attached both scripts... in the video, he just attached the player script only...
    is it bcoz he's using older / different unity version?
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,506
    actually i had that problem also, it didnt automatically attach the required component,
    if the script was already attached to the gameobject when you wrote it..

    if you remove Player_Scrpt and attach it again, it will automatically add PlayerCtrller.cs also, because of this line:
    Code (CSharp):
    1. [RequireComponent (typeof (PlayerCtrller))]
     
  5. chipp_zanuff

    chipp_zanuff

    Joined:
    Jul 26, 2016
    Posts:
    28
    but how could that happen? so i should attach and detach it first to make it auto attach the related script the next time i attach it?