#It s the same error whenever my raycast

1 messages Ā· Page 1 of 1 (latest)

outer dome
#

If you are using the ToonObject more than once it might be betetr to just TryGetComponent once from the first raycast, that way you don't need to try and get it multibel times

charred dock
#

Alright, I tried this but it had the same result as "GetComponent"

#

As in, the same error

outer dome
#

Ok just a sec

charred dock
#

Alright.

outer dome
#

Something like this maybe

charred dock
outer dome
#

In your code you still had the TryGetComponent in the if-statement

charred dock
outer dome
charred dock
#

Ohh, I must’ve forgot to remove that. Yeah it says ā€œalready defined within this scopeā€ and it causes a compiler error anyways.

I don’t think the issue is with the get component anyways, since the ā€œif (input.getbuttondown)ā€ statement doesn’t return any errors, only the other two statements below it.

outer dome
#

The two last if-statements?

charred dock
#

No, only the two last if statements that reference ā€œpaintableā€

The ā€œif (painted.collider != currentTargetā€ can be ignored, sorry for the confusion

outer dome
#

Ah ok, ill take a look again

charred dock
#

It’s fine, it was my bad

outer dome
#

Oh its because you try to get it, if you don't get it you still move on to the if statements, ```cs
// Basically this is kinda what is going on
TryGetComponent(out ToonObject paintable) (null)

if (paintable.triggered) (paintable == null)
// If paintable == null from the beginning you cant get anything from them but you still try to get something

#

Not sure how good of an explanation this is

charred dock
#

So, I need to "return" or do something similiar if I don't get it?

outer dome
#

Im not sure if this would work, but could be worth a try

if (Physics.Raycast(finder, out painted, range, obstructionMask) && TryGetComponent(out ToonObject paintable))```
charred dock
#
painted.transform.TryGetComponent(out ToonObject paintable)
if (paintable == null)
{
   return;
}

if (paintable.triggered)
{
}
outer dome
charred dock
#

thank you so much, this was causing 1000s of errors per second, it should work perfectly now!

#

yep, it works. once again, thank you so much for your time.

#

i'll also try your method instead of using return because it seems like better practice