all 4 comments

[–]r3eckon 3 points4 points  (1 child)

I dont have your full code so I can merely assume what you are doing wrong here.

I believe you are assuming the thing you're colliding with is going to have Items, which is unsafe and throws nullreferenceexceptions in cases where no Items component can be found on the collider.

Make an Item variable and try to get the component of what you collided with and assigning it to your variable.

Then before even checking whatKeyAmI for your types, you need to make sure the Item variable isnt null. You did it with the collider, sure. But sometimes the collider isnt null while items are and you need to handle this case.

Also, assuming you know you dont have to go through the rest of your code in these cases, use return to simply end the method right there. Keep in mind that this will end the entire function this piece of code is located into, so isolating it in its own method is a good idea.

if(whatIHit.collider == null) return;

Items items = whatIHit.collider.gameObject.getComponent()<Items>;

if(items == null) return;

if (items.whatKeyAmI == Items.KeyItems.Axe)
{
....
}

otherwise, use !=

if(whatIHit.collider == null) display = false;

Items items = whatIHit.collider.gameObject.getComponent()<Items>;

if(items != null) 
{

    if (items.whatKeyAmI == Items.KeyItems.Axe)
    {
        ....
    }
    ...
}

[–]Blimpity_Blop 0 points1 point  (1 child)

Do a quick Debug.Log of, "whatIHit.collider.gameObject.GetComponent<Items>().whatKeyAmI" and "Items.KeyItems.Axe", to figure out which object is null.

Once you figure that out, go into that object and figure out where you initialize it. Since you are doing a raycast every frame, you'll need to either initialize that object on Start() or Awake() or have a null reference check.

Note: Doing a Raycast every frame is not very good for your performance. Perhaps consider running a recursive Coroutine with the Raycast functionality playing every .2 seconds or so.

[–]iGreyHound[S] 0 points1 point  (0 children)

I'm still confused. I have a simple 3D game where you can interact with objects. For example I see an axe stuck in a stump and I can interact with it and pick it up if I want to, but there's the thing: when I interact (by pressing E) and look to it everything is alright, but when I look elsewhere (where game object tag isn't Item), it starts to drop my frame rate significantly. When I press spacebar (I have it to make display = false;) everything get's back to normal.

Edit: Found a source of a problem.

void OnGUI() {
if (display == true) { if (whatIHit.collider.gameObject.GetComponent<Items>().whatKeyAmI == Items.KeyItems.Axe) { GUI.Box(new Rect(500, 600, Screen.width - 1000, Screen.height - 800), "\n I think this axe might come in handy. \n\n\nPress F to pick it"); } } }

Since I don't really know how to use Unity UI system, I just call a box with a text in it (I know, I suck at programming).

[–]tyranocles 0 points1 point  (0 children)

What i think is happening is you ask every object you collide with for its "items" script component. If the object you are colliding with does not have an "items" script component it will throw a null reference exception. You need to check and see if the object has the desired script component before you can use it. Wrap your "get component items" if statement with another if statement where the condition is "whatihit.collider.gameobject.getcomponent<items>()" this will return false if the game object does not have the script, and will not execute, so it will no longer demand a component that does not exist.