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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

My script wont work anymore after i change var Player : Transform; to var Player : GameObject;

Discussion in 'Scripting' started by ArvinAdila, Nov 22, 2015.

  1. ArvinAdila

    ArvinAdila

    Joined:
    Nov 7, 2015
    Posts:
    3
    So here's my script

    var GhostWall : GameObject;
    var IsBuilding = false;
    var Player : Transform;
    var ghostclone : GameObject;

    function Update ()
    {
    if (!IsBuilding)
    {
    if (Input.GetKeyDown(KeyCode.P))
    {

    ghostclone = Instantiate(GhostWall, Player.position, Quaternion.identity);
    IsBuilding = true;
    }
    }
    else if (IsBuilding)
    {
    if (Input.GetKeyDown(KeyCode.P))
    {
    Destroy(ghostclone);
    IsBuilding = false;
    }
    }
    }

    Yeah this script works fine. But after I change the var Player : Transform; to var Player : GameObject; it give me an error where is says NullReferenceException: Object reference not set to an instance of an object. tell me what should I do.And im open to any suggestion .
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    you need to re-assign the player object in the inspector
    also note that you will need to change "Player.position" to "Player.transform.position"
     
    ArvinAdila likes this.
  3. ArvinAdila

    ArvinAdila

    Joined:
    Nov 7, 2015
    Posts:
    3
    yes .it does work hahaha .ive done a mistake at Player.position . haha .Thanks :)