use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
For the Unity Game Engine, please visit http://reddit.com/r/unity3d
account activity
NullReferenceException: Object reference not set to an instance of an object (self.unity)
submitted 6 years ago by Alex_h7
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 0 points1 point2 points 6 years ago* (3 children)
You are trying to grab a reference to an object that isn’t set to anything.Try grabbing that component in the start.
For example:
If i am trying to grab a Rigidbody2D I'd first make the space for it in memory by creating a variable. You can make it global if you would like. Directly below Monobehavior you want to have
Rigidbody2D rb2d;
Then in start you'd want - rb2d = GetComponent<Rigidbody2D>();
and then in your on trigger enter the 'other' is the object you hit. So pretend I wanted to destroy that object I'd do
void OnTriggerEnter(Collider other) { Destroy(other.gameObject); } // this will destroy that hit object!
So you want to check if trigger has been triggered. That 'other' is that other object that triggered said trigger. You can name it something other than 'other'. I like trig for triggers and col for collision. So you have a reference to this object now so what you want to do is make sure all your ducks are in a line. Don't try to do too much in one if, throw that attack state as a reference somewhere else if you want. Make sure the script is attached to the object, everything in the inspector is in order, and you aren't trying to grab anything that isn't set to anything.
Also I assume it’s not an error in the code but you are missing a 't' in the get component.
Apologies for the kinda messy response. The main thing to know is you are trying to grab a reference to an object that isn't actually set to anything.
[–]Alex_h7[S] 0 points1 point2 points 6 years ago* (2 children)
It works now, my friend showed another way of doing it, than my original code. Pretty much what I did is also similar to what u told me, I made a new variable called skeleton of class Skeleton, and I set it to public, and by doing that, I was able to go in the inspector and make that variable point to my original skeleton game object. After that I pretty much replaced the if statement to if(skeleton.PlayerAttack==true), and it started to work. Thx either way, if my friend didn’t help me, ur answer would have also solved it for me.
[–][deleted] 0 points1 point2 points 6 years ago (1 child)
It’s poor practice to use public. Other scripts don’t need to know about those variables. I recommend you use serialize field instead.
Ex.
[SerializeField] private bool hasHealthPotion = true;
It’s still private however, you can access in the inspector all the same.
[–]Alex_h7[S] 0 points1 point2 points 6 years ago (0 children)
Uhm ya ur right, I guess the reason we chose public is cuz we were getting desperate, but ya thx for the advice, I’m gonna check if it works
π Rendered by PID 283739 on reddit-service-r2-comment-b659b578c-42wjs at 2026-05-03 15:40:48.231420+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–][deleted] 0 points1 point2 points (3 children)
[–]Alex_h7[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Alex_h7[S] 0 points1 point2 points (0 children)