[HELP]Please resolve this error

I don’t know what arguments to put SpawnBullet method.

seven_sem1

Hi Manthan,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see which line gets highlighted. If you exposed a field in the Inspector, make sure that it’s not empty.

Hope this helps :slight_smile:


See also;

Sorry mam

void CheckForShot () {
    if(Input.GetKeyDown(KeyCode.LeftControl) && GameObject.FindGameObjectsWithTag("Bullet").Length <= 3) {
        SpawnBullet(transform.position);
    }
}

void SpawnBullet (Vector2 pos) {
    GameObject bullet = Instantiate(prefabBullet, transform.position, Quaternion.identity) as GameObject;
    bullet.GetComponent<Bullet>().ApplyForce(thrustDirection);
}

Thank you. I’ve edited your post to format the code properly.

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see which line gets highlighted. If you exposed a field in the Inspector, make sure that it’s not empty.

Mam actually I cannot find the argument in the function, what should be replaced in this line

void SpawnBullet (Vector2 pos)

Double click on the error message. Which line gets highlighted? I doubt that it is the declaration of the method.

I think the issue is either the line with prefabBullet or the following line. Check your Inspector. Is a prefab attached to the field named Prefab Bullet? If so, does the prefab have got a Bullet component attached?

Privacy & Terms