Brackeys Multiplayer FPs Tutorial Ep.6 Error Object Reference not set

Updated on September 4, 2018 in [A] Multiplayer
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
22 on August 25, 2018

I’m following his Multiplayer FPS Tutorial right now, but I’m stuck with the shooting part with the error NullReferenceException: Object reference not set to an instance of an object
PlayerShoot.Shoot () (at Assets/PlayerShoot.cs:37)
PlayerShoot.Update () (at Assets/PlayerShoot.cs:30)

The Error occours when I attempt to shoot in the running game

The 2 lines it points to are the Shoot() function and the other is a line in the declination of the Shoot() function, so I’m assuming its something with this function. 

 

 
using UnityEngine.Networking;
using UnityEngine;
 
[RequireComponent(typeof(PlayerMotor))]
public class PlayerShoot : MonoBehaviour {
 
 public PlayerWeapon weapon;
 
 [SerializeField]
 private Camera cam;
 
[SerializeField]
 private LayerMask mask;
 
void Start()
 {
 if (cam == null)
 {
 Debug.LogError("Playershoot: No Camera referenced");
 this.enabled = false;
 }
 }
 
void Update()
 {
 if (Input.GetButtonDown("Fire1"))
 {
 
//This is line 30, which is the function declared down, so I think its the cause of something in the shoot function
 Shoot();
 }
 }
 
void Shoot()
 {
 RaycastHit _hit;
 
//this is the line 37, which seems to buy the cause of the problem
 if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range, mask))
 {
 Debug.Log("We hit" + _hit.collider.name);
 }
 }
}
 

 

I hope someone can help!

 

Kind Regards,

 

Klofessor

  • Liked by
Reply
0 on August 25, 2018

I know many people had this problem, but no one had it with the shooting part and the other solutions aren’t for this particular code, so they didnt work

  • Liked by
Reply
Cancel
0 on August 27, 2018
  • Liked by
Reply
Cancel
0 on August 27, 2018

Did you set all the correct objects in the inspector for the script? Your weapon, cam or mask (game)object references are not correct. Did you debug the values before using and checked them?

  • Liked by
Reply
Cancel
0 on August 28, 2018

I put in my Cam, but I cant drag anything into the player inspector tab

  • Liked by
Reply
Cancel
0 on August 29, 2018

Then your ‘weapon’ and ‘mask’ variables are never set to anything else then their initial values, and they are ‘null’ or empty if you give it a name. In the turorial, did it work for brackeys? Or did you test inbetween because I think lateron the values will be set to the correct values.

  • Liked by
Reply
Cancel
9 on August 29, 2018

I tested at the moment he tested, I can put in my cam, but nothing seems to fit in the gun inspector tab

Helpful
on August 30, 2018

Do you see this in your inspector for the ‘player’ object under your created ‘player shoot’ script?

on August 31, 2018

See what? I tried to pull in any object I had, nothing fitted, thats what I meant

on August 31, 2018

See what? I just cant drag anything into the weapon inspector tab

Wise
on September 2, 2018

You can only bring PlayerWeapons into that slot.

on September 2, 2018

How do I create/change something to a PlayerWeapon

Wise
on September 2, 2018

It’s the PlayerWeapon script (component). Anything with that you can drop on it, and it will just use the PlayerWeapon script from it.

on September 2, 2018

I added the Playerweapon script to my player, still cant pull anything in

Wise
on September 2, 2018

You can bring the Player in. You can bring in things with the PlayerWeapon component on them. If the player has the component on them, then you can bring them in.

on September 3, 2018

I attached the Playerweapon script, its on the player. If I try to pull the player in the weapon slot the circle with the diagonal circle in the middle appears and when I release my mouse button it just disappears. If I drag my Player in the player prefab “box” a blue border appears, which isnt the case in the weapon box. So I guess its the right thing to pull in if there is a blue border? I also tried the little circle behind the box, it just says none in the window that pops up

Show more replies
  • Liked by
Reply
Cancel
0 on August 31, 2018

Hi Klofessor.

What you provide info is wonderful. personally, I feel below info also would help for you.

Learn Unity game design & 2D & 3D game development & make your own C# games in Unity 3D.

This is the most comprehensive course on Unity 3d on the Internet. We are avid game developers and were tired of all the junk out there — teaching students how to make 3D cubes without real-world game development.

This course will take you down a guided learn Azure You’ll learn to code in C# and then go on to build 2D & 3D games.

Not only you will build games, but you will learn how to do it the way the pros do. It’ll even cover advanced topics like lighting, cinematics, and multiplayer.

Here are some of the things you will learn:

Installation & setup
Physics
Pathfinding
Basic artificial intelligence
Lightning & lightmapping
Camera animations & cutscenes
Character animations
Scripting, scripting, scripting!
Materials & skyboxes
Sound FX & music
Multiplayer over LAN & Internet
Particle FX
Melee & ranged battle systems
Tower Defense Game
Adventure Game
Zombies!
9. Master Unity By Building 6 Fully Featured Games From Scratch
Learn How To Plan, Design, Create And Publish Your Games On Any Platform Using Unity Game Engine.

The Azure course is completely project-based, and we are going to create 6 fully featured games from scratch using Unity Game Engine. It will start with simple things so you will be comfortable if you are using Unity for the first time.

We are going to start with a simple game that I call Jack The Giant. In this game we are going to help Jack climb down the beanstalk, along the way we can jump on white clouds but we need to avoid dark clouds. Besides of creating our gameplay we are going to create menus, implement background music, implement admob ads and much much more.

The next game that we are going to create is Flappy Bird, and I know what you are thinking oh not another Flappy Bird clone, but this is not a regular Flappy Bird clone, oh no, I’ve added a lot of features that you can’t find in any other flappy bird clone such as selecting a bird(choose between different flappy birds), implementing tweeter and many other features that you will see inside of the course.

After Flappy Bird we are going to create one of the most popular games out there — Infinite Runner!! In this game we are going to see how can we implement features such as quads to represent our backgrounds, spawning obstacles and destroying them, recycling obstacles, implementing google play game services and much much more.

Next game that we are going to create is going to be a 2D Plaformer game that I call Spider Cave. In this game our player is able to fly and he needs to collect diamonds in order to exit out of the level, what he needs to avoid though are the spiders that lure inside the cave. We are going to have a spider shooter who shoots our player, spider jumper who tries to eat our player when the player is above him and spider walker who simply walks left and right. For the first time we are going to implement simple AI(Artificial Intelligence) in our games, and you can use these techniques in your own games that you will create!

After our spider cave we are going to create a nice puzzle game, which a kids game. In this game we are going to implement a lot of features such as creating the whole game in one single scene!! We are going to see how can we compare images to each other in order to check if we have a correct guess, we are going to use the mechanism system a lot in this game in order to animate our menus, that and much more awaits you in this lecture.

The final game that we are going to create is a game that is called Ball Panic. This is the largest game that we are going to create in this course and the game covers a lot of topics everybody is interested in, such as creating a level and character lock/unlock system, creating weapons menu in order to select a weapon for our player, creating level menu and unlocking those levels, implementing video and interstitial ads and in-app purchase, that and much more awaits you in this lecture.

  • Liked by
Reply
Cancel
0 on August 31, 2018

So I’m really going to get advertsing in my forum post?

  • Liked by
Reply
Cancel
0 on September 3, 2018

I solved my problem! I replaced some parts of my script with my own coding and some of brackeys stuff. I would recommand the Shooting Raycasts tutorial from Brackeys and for just the core shooting only watch 0.00- 4.52. It solved my problem

Heres the link to the video for anyone with the same problem:

https://www.youtube.com/watch?v=THnivyG0Mvo

Thank you to anyone trying to help me 😉

  • Liked by
Reply
Cancel
2 on September 3, 2018

Now I have another problem :/ I’m still on episode 6, where he codes the gamemanager script. It doesnt recognize the word Player in my visual studio, but it does in his

Wise
on September 4, 2018

Do you have a class named “Player”

Spelled EXACTLY like that.

 

on September 4, 2018

Thanks, I’m an idiot. My class is called differently thanks again 😉

Show more replies
  • Liked by
Reply
Cancel