#NullReferenceException Object reference not set to an instance of an object

1 messages · Page 1 of 1 (latest)

trail oak
#

Can anyone please help me, i dont understand what the problem is here. The Thread name is the error i keep getting in console. I will appreciate anyones help ❤️
Players Script --- ```public class PlayerController : MonoBehaviour
{
public float playerSpeed;
private Rigidbody playerRb;

public BulletScript bulletScript;

void Start()
{
    playerRb = GetComponent<Rigidbody>();
    bulletScript = GameObject.Find("Bullet").GetComponent<BulletScript>();
}

void Update()
{
    //Move Player Vertical And Horizontal
    float forwardInput = Input.GetAxis("Vertical");
    float sidewaysInput = Input.GetAxis("Horizontal");
    playerRb.AddForce(Vector3.forward * forwardInput * playerSpeed);
    playerRb.AddForce(Vector3.right* sidewaysInput * playerSpeed);

    // If Mouse Left is Clicked, call function from BulletScript
    if (Input.GetButtonDown("Fire1"))
    {
        bulletScript.Shoot();
    }
}

} ```
Bullet Script ---

{
    private Rigidbody bulletRb;
    private GameObject player;
    public GameObject bulletPrefab;

    private float bulletVelocity = 5;
    // Start is called before the first frame update
    void Start()
    {
        bulletRb= GetComponent<Rigidbody>();
        player = GameObject.Find("Player");
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void Shoot()
    {
        Vector3 playerPos = player.transform.position;
        Instantiate(bulletPrefab, playerPos, bulletRb.transform.rotation);
        Debug.Log("YOU SHOT!");
    }
} ```
twin sapphire
#

does it say from which script it originates (the error)?

naive knoll
trail oak
#

Player Controller.cs:29

twin sapphire
#

which line is that lol

#

i cba to count

trail oak
naive knoll
#

use a link or site to post code so we can view the full thing instead of code in here without lines . . .

#

this should be known if using the general (intermediate) coding channel. please check the #854851968446365696 on how to post code . . .

trail oak
#

ok will do sorry

twin sapphire
# trail oak

again i am not too sure which on is line 15 but i am guessing its the .find("bullet")

#

is the spelling right?

naive knoll
# trail oak

again, use a link. we can't tell what line . . .

twin sapphire
naive knoll
#

not everyone can see the code, only if they're using a computer . . .

twin sapphire
#

i counted

naive knoll
#

we shouldn't have to in order to help. that's the sad part. this is a #💻┃code-beginner issue. the proper details should be provided for everyone to assist . . .

twin sapphire
trail oak
#

Sorry for being slow

twin sapphire
naive knoll
naive knoll
#

error on line 15 because when you find the "Bullet" game object you immediately access it to get the BulletScript component, but if it did not find the "Bullet" game object, the returned value of GameObject.Find("Bullet") is null. you can't access anything or use GetComponent on smth that is null . . .

twin sapphire
# trail oak updated

do you have a prefab called "Bullet" with the script bulletScript attached to it?

trail oak
#

yes

twin sapphire
#

ok

#

do you spawn the bullet at the start of the game?

#

or only when the player shoots?

naive knoll
#

error online 29 because as previously mentioned if GameObject.Find("Bullet") is null, then GetComponent<BulletScript>(); will return null. you access bulletScript on line 29 . . .

trail oak
#

Only when mouse left is clicked

twin sapphire
#

what is happening is that at the start of the game the script tries to find this obj but cant

#

therefore errors out

#

what you need to do is first spawn the obj then look for it

trail oak
#

Ah ok. Will make some frustrations but ty.

twin sapphire
#

np

naive knoll
#

@trail oak why does the PlayerController need to find the Bullet game object in the first place?

#

this part doesn't make sense to me . . .

trail oak
#

The bullets mechanics will be placed in there. the bullet script which is attached to the bullet

naive knoll
#

place in where, the PlayerController script?

trail oak
#

Im trying to make a bullet for my player to shoot without looking at tutorials,to challenge myself

naive knoll
#

if so, that doesn't make sense . . .

naive knoll
trail oak
#

Hmm, So if i press left click, should i instantiate and move the bullet all inside the playercontroller?

naive knoll
#

war is obviously bad and we all hate it, but i doubt every soldier is shooting and moving each bullet shot by themselves . . .

trail oak
#

Smart! OK i will look at those perspectives next time to make better code xD, that would be hectic if bullets moved themselves.

naive knoll
#

if practicing by yourself. a good note is that every object should take care of it's own functionality . . .

naive knoll