NullReferenceException error

Hey, while coding I had a problem… the problem was error actually
“NullReffernceException: Object reference not set to an instance of an object TileManager.Start() (at Assets/Scripts/TileManager.cs:16)”
I checked it like 10 times nothing wrong everything is right… I don’t know what have I done wrong in the code :

using UnityEngine;
using System.Collections;

public class TileManager : MonoBehaviour
{
    public GameObject[] tilePrefabs;

    private Transform playerTransform;

    // Use this for initialization
    void Start () {
        playerTransform = GameObject.FindGameObjectWithTag ("Player").transform;
    }
   
    // Update is called once per frame
    void Update () {
   
    }
}

While I’ve created EmptyChild named as TileManager and putted this script on it and in the size of “Tile Prefabs” I’ve putted 5 and clicked enter… 5 empty columns appeared and putted in them 5 prefabs.

What have I done wrong? I don’t find any error I’ve done why this is keep appearing, the code is basically balanced.
if anyone know what should I do… I will appreciate his help!

Thanks,Respectfully.

is that your entire script, because it’s throwing the error from line 16 within start… in code you’ve pasted start ends by line 14 and line 16 is a comment…

1 Like

Is there a tag on the player? why is the player transform private? just make public and drag the player into it? :open_mouth:

1 Like

Never seen a comment throw a null exception before. :smile:

1 Like

No I was just trying to re-edit it so many times but didn’t click start so this happens xD

using UnityEngine;
using System.Collections;

public class TileManager : MonoBehaviour
{
    public GameObject[] tilePrefabs;

    private Transform playerTransform;
    private float spawnZ = 0.0f;
    private float tileLength = 10.0f;
    private int amnTilesOnScreen = 7;

    // Use this for initialization
    private void Start () {
        playerTransform = GameObject.FindGameObjectWithTag ("Player").transform;

        for(int i = 0; i < amnTilesOnScreen; i++)
        {
            SpawnTile ();
        }
    }
   
    // Update is called once per frame
    private void Update () {
        if (playerTransform.position.z > (spawnZ - amnTilesOnScreen * tileLength))
        {
            SpawnTile();
        }

    }

    private void SpawnTile(int prefabIndex = -1)
    {
        GameObject go;
        go = Instantiate (tilePrefabs[0]) as GameObject;
        go.transform.SetParent (transform);
        go.transform.position = Vector3.forward * spawnZ;
        spawnZ += tileLength;
    }
}

Now there is 2 errors :
“NullReffernceException: Object reference not set to an instance of an object TileManager.Start() (at Assets/Scripts/TileManager.cs:15)”
“NullReffernceException: Object reference not set to an instance of an object TileManager.Update() (at Assets/Scripts/TileManager.cs:25)”
I don’t know what’s wrong with playerTransform itself :frowning:

I don’t know… I thought making it private make it more readable… My “Player” is untagged, Should I tag it with “Player” or tries public and drag player to it better?
I dunno yet :frowning:

This how luck works :confused:

Your “GameObject.FindGameObjectWithTag(“Player”)” function call is failing to find a GameObject with the tag “Player” in your scene.

2 Likes

Alright thanks A LOT !!! <3 My player was untagged so I tagged it “Player”

I had another problem I don’t know if I should make another thread or something but let’s ask nothing wont happen :slight_smile:
My ball is flying like 0.5cm from the ground, however when I try to increase the “Y scale” of my ground to don’t let the ball fly when I just start the game it fly again… before I start the game the ball is standing on the ground.
Can you help me out please? sorry for bothering :smile:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{

    private CharacterController controller;
    private Vector3 moveVector;

    private float speed = 7.0f;
    private float verticalVelocity = 0.0f;
    private float gravity = 12.0f;

    void Start()
    {
        controller = GetComponent<CharacterController> ();
    }


    void Update()
    {
        transform.Rotate(6, 0, 0);
        moveVector = Vector3.zero;

        if (controller.isGrounded)
        {
            verticalVelocity = -0.5f;
        }
        else
        {
            verticalVelocity -= gravity * Time.deltaTime;
        }

        // X - Left and Right
        moveVector.x = Input.GetAxisRaw("Horizontal") * speed;


        // Y - Up and Down
        moveVector.y = verticalVelocity;

        // Z - Forward and Backward
        moveVector.z = speed;

        controller.Move(moveVector * Time.deltaTime);
    }
}

Because I want to make it Rigidbody but it doesn’t even touch the ground also I want the ball itself to keep moving forward moveVector.z = speed;
so what should I do :3

If you don’t want the ball to move up or down, just change your line of code that says
'moveVector.y = veticalVelocity;"
to
‘moveVector.y = 0.0f;’

That should fix your problem of the ball moving up or down.

Nope

"If I’m on the ground, move me up (well down, but you’re taking away a negative gravity, so I think it’ll flip the sign) 0.5f units… " if you don’t want it to be 0.5 units off the ground, dont do this :stuck_out_tongue: