#Hey I need some help please The code for

1 messages ยท Page 1 of 1 (latest)

crimson tulip
#

How about a thread ๐Ÿ™‚

astral knoll
#

perfect โค๏ธ

crimson tulip
#

So, can you explain which line of code on what script is actually firing the error? I just got confused by all those parts ๐Ÿ˜„

astral knoll
#

here I can collect and even use all the items. Each item in the inventory above has a scriptable Object attached. At this point everything is already in the dictionary so now when I click on the weapon it goes to the equip slot at the bottom. Then the weapon needs to spawn in the characters hands.

#

all I need to do is read the ScriptableObject attached to the inventory slot to read the prefab.
Then instantiate the prefab into the charactes hand.

I do this in the first file I have shared.
https://gdl.space/zuyazubuna.cpp

And then the error appears

crimson tulip
#

on line?

astral knoll
#

on line 49 to 53 that I commented out

crimson tulip
#

Okay, just super simple question. Did you forget to assign a itemPrefab on one SO? ๐Ÿ˜‰

astral knoll
#

ahahahah No XD

crimson tulip
#

Could have been ๐Ÿ˜„

astral knoll
#

cause it pops up in the video for half a second

crimson tulip
#

wait, it pops up?

astral knoll
#

yes.

crimson tulip
#

can you debug.log instantiatedItem.transform please?

astral knoll
#

sure

#

I've even added my own debug to ensure I have the scriptableObject attached. Something is weird here.

crimson tulip
#

the selected log is the one of transform?

#

and if you now alter the position, it bugs out?

#

wait, the code is not the issue there. It is running fine. the log gets hit with "item is now equipped"

astral knoll
#

well it bugs out regardless. when I remove the code alltogther it's fine again

#

Ya

#

it's super weird

crimson tulip
#

select the stacktrace again of your error pls

astral knoll
#

have a look at second 04

#

3 and 4 sorry

crimson tulip
#

yeah, I did. can you select your error and show the stacktrace?

astral knoll
#

I am more than happy to jump into a voice channel if you are comfortable

#

sure 1 sec

crimson tulip
#

InventoryManager line 43, what is this doing again

astral knoll
#

Adding

crimson tulip
#

and cant go voice, hopping back and forth with clients on google meet ๐Ÿ˜„

astral knoll
#

wait!

#

that might be it!

crimson tulip
#

yep...

astral knoll
#

when equipping the item It tries to add it again and it can't becase this time there was no item collected

#

hmmmm

crimson tulip
#

also do not call your object gameObject, because gameObject is already representing your actual gameObject the component is attached to ๐Ÿ˜‰

#

ohhh, and on next line you try to get the component, but on the wrong gameObject I guess ? ๐Ÿ˜„

#

But if you say line 43, then either the dict or the itemCollected might be null

astral knoll
#

in what class sorry ?

crimson tulip
astral knoll
#

Ah, that does seem right but It still does not make sense to me. Why would the only differnce be me isntantiating a game Object and me just equipping it break things.

#

check this out:

#

Removing this

#

I can now equip the weapon

#

and use it

crimson tulip
#

What? Are you instantiating it two times? ๐Ÿ˜„

astral knoll
#

so now the millioin dollar question is, why on earth can I not just add the gameObject to the world. ๐Ÿ˜‚

crimson tulip
#

Is your item doing something on generating it?

astral knoll
#

no, my thought process would be, I pick up the item
It gets added to the inventory slot where a copy of the scriptableObject also is stored. Upon equipping the item, pass the SO to the instantiation code to get the prefab to spawn it into the players hands.

#

To me this should be simple right ? but yet I am having a hard time with it

crimson tulip
#

I am wondering why you do all that casting

#

your iteamScriptableObject is already type of Item

astral knoll
#

Oh, I have item SO as the parent and then I have a weapon SO, throwable SO , consumable SO

crimson tulip
#

itemScriptableObject.itemPrefab should be enough to call

astral knoll
#

I just pass in the generic SO and cast to item or weapon or consumable

crimson tulip
#

(Item) should be enough

astral knoll
#

Okay. Well, let me give this another try by removing the casting and then see what happens

crimson tulip
#

and can you do this, without any var and stuff. just instantiate and see what happens

Instantiate(itemPrefab, GameObject.Find("Hand.R").transform, true);
astral knoll
#

Sure. Let me try that quick

#

That works but I don't see it pop into the world

crimson tulip
#

like in the hierarchy?

astral knoll
#

Hold up! It is there!!!!

#

๐Ÿฅณ

crimson tulip
#

๐Ÿ˜„

#

now lets revert back to your code and see, what could be the issue

#

so what was your current code thats working?

astral knoll
crimson tulip
#

That is far off ๐Ÿ˜„

astral knoll
#

at 0. 0 .0

crimson tulip
#

Wait, your inspector says x = 520

#

because you dont set it to vector3.zero yet?

astral knoll
#

๐Ÿคจ

crimson tulip
#

I am confused by your setup ๐Ÿ˜„

astral knoll
#

I am confused by my setup!

#

๐Ÿ˜‚

crimson tulip
#

Okay, anyway. lets see what happens if we set the localposition to zero ๐Ÿ˜„

astral knoll
#

XD alright

crimson tulip
#
GameObject instantiatedItem = Instantiate(itemScriptableObject.itemPrefab, GameObject.Find("Hand.R").transform, true);
instantiatedItem.transform.localPosition = Vector3.zero;
astral knoll
#

In my defence this is the first big project I am tackeling and I have a devlog series up on the tubes about everything I have so far .

#

I can't store the instantiated object as a gameObject strangely enough

crimson tulip
#

Oh nice, always cool to see the progress on a project ๐Ÿ™‚

#

do you get an error?

astral knoll
#

so that might have been the issue

crimson tulip
#

what the heck, why cant you get an gameobject back from instantiate ๐Ÿ˜„

astral knoll
#

The returned item is a object

#

I need to cast it for me to get a Go

crimson tulip
#

Weird, feels like I never used unity on the simplest things right now ๐Ÿ˜„

astral knoll
#

I'm doing it fine in the inventory manager for the item slots

crimson tulip
#

What is your itemPrefab? what type, its gameobject or item?

astral knoll
#

it's a transform ๐Ÿ˜•

#

No

#

GameObject

crimson tulip
#

Things start to get super weird here ๐Ÿ˜„

astral knoll
#

The prefab attached to the SO

#

maybe I should just remove everything I don't need and try again

crimson tulip
#

what if you instantiate as GameObject, does this work at least?

astral knoll
#

so cast to type

#

let me try

crimson tulip
#

Just make var to GameObject and cast it and see if you get the error again

#

Gotta go in some minutes unfortunately, but will remain in this thread and check later. I wait for your response about the casting tho ๐Ÿ˜„

astral knoll
#

๐Ÿ˜’ Don't judge, I can instantiate as GameObject.. would help if I passed in the right type of SO though ....

crimson tulip
#

๐Ÿ˜„

astral knoll
#
#

That worked btw but

#

OMG

crimson tulip
#

subscribed btw, but what? ๐Ÿ˜„

astral knoll
#

DUde the other script worked and because it attached to my arm the character was triggering the pickup script and because it was instantiated without a gameobject you get the error.. Check this out hahah

#

hahahah I can't believe this

#

the code was working all along. It was my dumb ss not using a clean gameObject

#

๐Ÿ˜…

crimson tulip
#

Yeah, using vars can just lead to missing casting and recognition of wrong types ๐Ÿ˜„ I try to avoid var as much as I can ๐Ÿ˜„

#

You might wanna simplify your script if possible at some point ๐Ÿ˜„ Alrighty, gotta go, time for some dad duties over here. see ya later ๐Ÿ™‚

astral knoll
#

Thank you so much. I hope to talk again soon

#

keep safe and thanks again

crimson tulip
#

Same to you and always a pleasure to help. Happy developing ๐Ÿ™‚

astral knoll
#

๐Ÿฅณ Real MVP

astral knoll
#

Hey, going to just drop this here . I am struggeling to get the rotation and position correct of the weapon I spawn. I can't get it to spawn into the hand correctly

crimson tulip
#

I am sure, thats a minor issue. And putting something in the correct position will hunt you across game dev anyway, so I would not avoid that ๐Ÿ˜‰