Can someone explain to me the invalid set index error?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Witcher64

I am trying to learn Godot and Gdscript by using the “Godot Game Engine Projects” textbook. I have copied the code exactly as it is in the textbook
func spawn_coins():
for i in range(4 + level):
var c = Coin.instance()
$CoinContainer.add_child(c)
c.screensize = screensize
c.position = Vector2(rand_range(0,screensize.x),
rand_range(0, screensize.y))
and i am getting the error: Invalid set index ‘screensize’ (on base ‘Area2D (Coin.gd)’) with value of type ‘Vector2’
also my indentions are correct they just don’t show up on here

What does your Coin.gd look like?

exuin | 2020-09-16 00:14

my Coin.gd is
extends Area2D

func pickup():
queue_free()

Witcher64 | 2020-09-16 00:47

To properly post code in the forum…

  • Copy properly formatted code to the clipboard
  • Paste it into your message
  • Select the code
  • Press the “{ }” button in the editor’s toolbar.

That’ll properly format it for the forum.

jgodfrey | 2020-09-16 14:30

:bust_in_silhouette: Reply From: exuin

my Coin.gd is
extends Area2D

func pickup():
queue_free()

Looks like your error is that you don’t have a variable screensize in your Coin.gd. Your code isn’t complete yet, so it won’t run properly yet. I think this is how your code should look.

so I added var screensize to my coin.gd and now everything spawns, but my coins dont disappear when i run into them (I thought this is what queue _free was for)

Witcher64 | 2020-09-16 01:18

Thanks for the help! I found out that i had my group (coins) attatched to my collision shape instead of my coin node. problem solved!

Witcher64 | 2020-09-16 01:38