#NullRefeferenceException Error
237 messages ยท Page 1 of 1 (latest)
line 19 is
var selectionTransform = hit.transform;, yes?
oh ya i forget discord doesnt show tghe lines
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
that is line 19?
to me, it seems that Camera.main is undefined
this is surprising, but you may have multiple cameras in the scene, which could cause that to freak out
it is probably better to directly define a camera variable, and reference that instead
oh shoot, I didn't even think to look at the video, where you clearly show line 19, apologies
Is the camera attached to the Player object, because i don't even see a camera object?
And if it is, make sure it has the "MainCamera" tag
Actually, i don't see the lines for the viewport of the camera at all anywhere.... is it so simple you've deleted the camera? ๐
what i didnt deletethe camera
Well, it's not in your hierarchy, unless you have it as a child of your Player object
ah, good catch, this is probably the culprit
the MainCamera tag, that is
NullExceptionError troubleshoot for me always ends up being one of these two:
- Does the object exist?
Yes? Move to two
No? Add it - Is the reference to the object correct?
Yes? (usually never if it's Yes on previous)
No, fix the reference so it catches the object correctly.
opk ill try that
could you just use a serialized reference, like I originally suggested?
it is probably better anyways, as then you don't have to rely on a single camera
I mean... i have a vague feeling he doesn't even have the camera there at all, since i can't see it in the hierarchy, and i don't see the viewport lines coming from the character so i get a feeling it's not attached to the player, wich is the only not-expanded object in the hierarchy
the camera is a child of Player, I guarantee it
So... the object in the scene is not the player then i assume?
which object in the scene, exactly?
Ah i guess the ball is "Sphere"
The player is nowhere close to where he's looking in the scene.
I thought it was a capsule, wich most people use as "Player" early on lol
heres the video
#survivalgame #tutorial #unity
In this tutorial series, we will create a 3D survival game with Unity & C# as the scripting language.
We will start with basic FPS movement, inventory building, and crafting systems.
Learn how to pick up items and more.
Little by little, we are going to add different features that are common to survival open-world...
But i'd probably still make a proper reference in the code for the camera. Either by tag "MainCamera" or by child of player, or just by a public field reference it in the inspector
Make sure i can always see the reference properly.
@shy bridge Is your Camera a child of your Player Gameobject? And does it have the MainCamera tag?
ill look
yes, we understand
we both have solutions to your problem, could you try one real fast?
do you understand what a NullReferenceException is? it may help you understand what we're instructing you to do
now it does
no
TL;DR a NullReferenceException is when your script can't find a GameObject you're telling it to use, so there reference of it is null
aka empty
kinda?
@vivid sundial It'd be the "nub" explanation
There's more to it
Like, i don't even know the full extent of things like that either ๐คทโโ๏ธ
But i feel like it's general rule of thumb that either your code can't find the object reference, or the object actually doesn't exist
it is simply indicative of being unable to find a reference to an object
not necessarily a GameObject, just any non-primative type
Well, true
Can be anything that is null when it shouldn't be, basically. ๐คทโโ๏ธ
@shy bridge Did you say you didn't have the MainCamera tag, and you do now?
Did that help?
ya
ok, good
i still get the error tho
I think he just said yes to the tag question
one c\sec
now im getting this error
interaction_text.text = selectionTransform.GetComponent<InteractableObject>().GetItemName();
null
yeah, ok, that's because not every object that the ray collides with has a InteractableObject component
you have to filter the results somehow
ok
you could do this in a few ways
- Guard the target code with a null check
- Tag your objects, and then guard with a tag check
- Put all of your interactables on a separate layer, and use a layer mask on the raycast
I just checked in the guide, it's wrapped in an if statement so unless i'm being dumb atm, it shouldn't toss an error
This is the awesome but also horrible thing about tutorials. if you follow every step exactly, it works. If you miss a tiny step somewhere, it becomes real annoying to find it, especially in the beginning of your game-dev experience before you've started getting used to what tosses errors
oh shoot, didn't even realize that
ok, then interaction_text must be undefined
I'm wondering... if it's issues with the reference to interaction_text
variables_written_like_this
Oh
Some people like it
Some people live and die on that hill lmfao
ALWAYS UNDERSCORE EVERYTHING
yes, however languages have standards
people not willing to put down their preferences for those standards are... questionable...
Truuueee, but technically this is user-specific.
You can name a variable asdfnizsdnfuisduifg if you prefer that
or 1896525
no, you can't name a variable 1896525
Probably not, but you know what i mean
You've quite some freedom to name variables like you want
Some bastard i saw who did a movement script, used "num" "num1" "num2" etc up to num5, in a whole movement script.... i figured out in the end that num and num1 was X and Y direction of mouse ๐คฆโโ๏ธ
@shy bridge Can you show/tell what you're doing when the error is tossed, and paste the whole script?
ok, I'm really starting to question this tutorial...
wrong naming conventions, using GetComponent in Start()
bro's probably going to use a Find method in a sec here
oh, and using public, not [SerailizeField]
hides
I've actually used both. But in my defense, atm, i'm just throwing something together that sorta works, before i start refining it.
here let me make a video
actually though, I'm really curious about who exactly began teaching people to use public like that
@vivid sundial ๐คทโโ๏ธ I've gone around most my scripts and set most to private and started referencing a lot by script rather than in the inspector. But i still have a bunch of publics left.
But i'm definitely doing GetComponents in my Start() methods a lot
Is one example.... hides
nooooooooooo
IT WORKS
NIO wAYYYYYYYYYY
no, bro, you're using a find method... that's the real issue here
@shy bridge Sorry i was talking to Wormy about that. BUT. In your case.....
Wheres the text component in the hierarchy?
on the Canvas?
*proceeds to show the player component and not the component in question*
@vivid sundial Is it just me that find the text referencing a bit strange in the script?
wdym
you mean the scripts
as I've told you, I believe the issue to be a missed reference to interaction_Info_UI
so I wanted to see if that was defined in the inspector
You've a text object somewhere in your hierarchy. The Text interaction_text; variable that you're trying to reference via interaction_text = interaction_Info_UI.GetComponent<Text>();
but there's neither private or public before Text interaction_text; wich irks me a bit, but idk if it's doable that way.
hey, by the way, you can create in-line code blocks by encapsulating text with the diacritic mark
What mark is that
so, like `code here`
Soooo... you figured it out and it works now? ๐
@vivid sundial Btw, since you didn't like my Find method... what'd you do? I know that some people out there tend to be "Never use Find by tag!" for example, but... it works

@shy bridge Okay so... there's an issue with the reference.
The gameobject you have the script attached to, show the inspector. Aka this part:
@vivid sundial ngl, i use a lot of Find by tag.... because it works for me cross-scene and i use a lot of instantiated prefabs 
Don't use Find methods!
Find methods in Unity are an often-tempting solution to referencing a scene object. However, there is always a better solution.
Whenever you call a Find method, Unity must traverse your entire scene hierarchy and check every single object until it finds a match; and the methods which return arrays will always traverse the entire hierarchy regardless.
This is inefficient! It means that the more objects you add to your scene, the slower these methods become; and it gets even worse if you are calling them multiple times.
A non-exhaustive list of Find methods to avoid:
https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html
https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html
https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html
To read about a better solution, type []getareference or []getref
@shy bridge interaction_info_UI is not a text component?
@little hearth , the brackey's bot can define why it's so bad better than I can, so there you go
you may have to do some reference shuffling, but there's always a better way to find references
@vivid sundial Alright... i'll keep it in mind and work that in later. For now, i won't touch it since i got other things to do.
AHA
Ahh, TextMeshPro is not Text
AAAAAAAAAHHHHHHHHHHHHHHHHHH
So! In your code, do this:
When you reference your text
(do not write private if you reference in the inxpector, without adding [SerializeField] on the line above)
So you'd like to do, in your case:
[SerializeField]
private TextMeshProUGUI interaction_text;
before you continue, I want to warn you against using this tutorial any further (or using one at all, but that's another argument)
it is clearly outdated (Text has been deprecated for quite some time now), and it is using very, very poor conventions, all around
im kinda new to the coding
I've been in the same seat and gone through the same struggles.
I'm going to guess you're using the guide because you want to make a survival game? @shy bridge
I understand
it may be helpful to look for a more up to date source, and a more reputable one
like a medieval type
but what do i do for this? interaction_text = interaction_Info_UI.GetComponent<Text>();
instead of using tutorials that tell you how to make a particular game, you should try to find one that helps you with a specific problem
what do you mean?
or for the Get for the GetComponent<Text>();
@shy bridge Yeah, figured. I wouldn't want to tell you to stop per se, but a survival game is a huge undertaking and there's a lot of systems that interact with each other.
When you're new to coding, i'd (from experience) try and narrow the scope and make smaller things, to start to get used to programming.
But, if you're insistent on making a survival game. Take it step by step instead of get a tutorial for a whole "game".
Start with say Playermovement.
Then one for something else simple.
Then a new guide for some other part. Step by step etc.
@shy bridge replace Text with TextMeshProUGUI i believe is all you need to do
Wormy is going to pull his hair now, but i do the same thing here essentially:
With that extra if statement, i also get an error in the log if the reference for the component is not found for whatever reason.
this works, but it would probably be better to use a serialized reference to that component, rather than working around its GameObject
@shy bridge Get used to making null-checks like that if statement in my picture, so you can track your missing references easily.
why does my mouse have a stroke
@vivid sundial I too, am an amateur and i don't know all the ins and outs. This works and the reason it is currently like this, is because i need to hard reference to a child of a prefab i instantiate into the scene.
It's ugly.... but it works
That's all i care about for the time being!
@shy bridge You mean the jump the first time you hover over the object?
ya
That's a good question... 
we would need to see the code for your camera controller to answer that question
I wonder....
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCam : MonoBehaviour
{
public float sensX;
public float sensY;
public Transform orientation;
float xRotation;
float yRotation;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void Update()
{
// get mouse input
float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * sensX;
float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * sensY;
yRotation += mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
// rotate cam and orientation
transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
orientation.rotation = Quaternion.Euler(0, yRotation, 0);
}
}
one out of Dave/ GameDev
I don't think the issue is there. Because it happens first when he hovers at the object
[]cb
Use codeblocks to send code in a message!
To make a codeblock, surround your code with ``` (3 backticks. Click here to see where the key is)
To use syntax highlighting, add the file extension of the language you wish to highlight (cs for C#, cpp for C++)
For example:
```cs
Console.WriteLine("Hello World");
```
Produces:
Console.WriteLine("Hello World");
To send lengthy code, paste it into https://paste.myst.rs/ and send the link of the paste into chat.
So, it happens upon the raycast hitting the object
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
var selectionTransform = hit.transform;
if (selectionTransform.GetComponent<InteractableObject>())
{
interaction_text.text = selectionTransform.GetComponent<InteractableObject>().GetItemName();
interaction_Info_UI.SetActive(true);
}
else
{
interaction_Info_UI.SetActive(false);
}
}
under this
`, not '
thanks
Perfect
is there any other input here, or does this just happen when you hover?
no click or anything, right?
But yeah, it's a bit strange it makes your camera jump, because your raycast doesn't affect your camera... ๐ค Even more strange was that when it also jumped, it didn't trigger showing the UI, but it did the next time
when i hover
Did you include the
else
{
interaction_Info_UI.SetActive(false);
}
yes, they did
Oh yeah i'm blind
But then there's an issue there too, it doesn't clear the raycast
Because the label stays when he looks off of it
so its has to be the raycast system in my SelectionManager
There's certainly something wonky with the raycast, but there might be something with the camera, or some other script that interacts with one or the other.
It's really hard to tell without being hands on with the project
true
In this case, i say play around with it and see if you can figure it out.
The more you play around with, break, repair, scratch your head, cry, leave the PC, take a shit, come back, cry a bit more, smash it with a hammer.... the more you learn!
that is a true statement
I spent 10-12 hours on an issue the other night.
I discussed the issue with someone in another discord and he just managed to shine some light on some areas i need to check
And the next day i woke up, went to PC, checked it, and i figured it out!
ok @shy bridge
I've looked through all the stuff you've shown thus far, and I have no idea what could be causing this
Yeah i'm unsure aswell, bit of a headscratcher honestly. Hence i said just... mess around with it. There's not that much to your project yet that can cause issues.
Messing around is afterall the best way to learn ๐คทโโ๏ธ
to start debugging this, I'd print the values of xRotation and yRotation and see if they change when the camera jumps
if they don't, it may be something to do with the player parent, not the camera
if they do change, something is probably messing with the input axis
also, you should really use the new input system, it's way better!
again, something this tutorial doesn't cover, but it should
Never show Wormy my code again 
Joke aside, thanks for clarifying the thing about Find earlier
@vivid sundial
If you'd like me to show you some better ways to do what you're doing, I can, I suppose
@vivid sundial One thing at a time lol. I'll figure it out eventually. Now i at least know about it.
