0 votes

Hello, I'm new to Godot and programming in general. I've been following this tutorial on Zenva and have put in the code exactly to make the enemy move back and forth. However, I keep on getting this error:

Invalid set index 'x' (on base: 'Vector2') with value of type 'Nil'

This is the code that I wrote (pretty much copied but yeah...). If anyone can help me, please try to explain it in the most basic terms possible.

extends Area2D

export var speed : int = 100
export var moveDist : int = 100

onready var startX : int = position.x
onready var targetX : int = position.x + moveDist

func _process (delta):
    position.x = move_to(position.x, targetX, speed * delta)
    if position.x == targetX:
        if targetX == startX:
            targetX = position.x + moveDist
            targetX = startX

# moves "current" towards "to" at a rate of "step"
func move_to (current, to,step):
    var new = current

    # are we moving positive?
    if new < to:
        new += step 
        if new > to:
            new = to
        #are we moving negative?
            new -= step 
            if new < to:
                new = to            
        return new
in Engine by (12 points)

Try moving last 5 lines of move_to one intendation to the left. Now the function doesn't return anything if new >= to

Thank you so much! That's exactly what it was.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.