attempt to call function 'set_offset' in base 'null instance' on a null instance

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

Hi, sorry if I do not understand is that I am Spanish and I am using the google translator

my problem is the

I want to make every 2 seconds an object (my enemy) be generated randomly between a place, everything is correct but I got the error of attempt to call function ‘set_offset’ in base ‘null instance’ on a null instance.
I am using kinemasticBody2D and I cannot use another. Help pls this is the code

    extends Node
var Roca = load("res://Enemigo.tscn")
export (int) var Score
onready var Enemigo = load("res://Enemigo.tscn")

func _ready():
func nuevo_juego():
	Score = 0

func _on_InicioTimer_timeout():
func _on_EnemigoTimer_timeout():
	var NewEnemigo = Enemigo.instance()
	NewEnemigo.position = $enemigos/Posicion.position
func _on_ScoreTimer_timeout():
	Score += 1

func _on_DisparoEnemigo_timeout():
	var DisparoEnemigo = true
func game_over():

func _on_Timer1_timeout():

I clarify that I am using a node for that but the enemy is a kinemasticBody2D

The func of the error is the following (it is in the complete code)

func _on_EnemigoTimer_timeout():
	var NewEnemigo = Enemigo.instance()
	NewEnemigo.position = $enemigos/Posicion.position


that is the line of error

:bust_in_silhouette: Reply From: TheNewGrant

I think I might see a problem. Right in “set_offset” in you have “randi()” but no random restraints or set of numbers to chose from.


It would be wise to use randi() % any number to set parameters to the random from 0 to that number.
Yo creo que yo veo la problema. Despues de “set_offset” tu tienes una “randi()” pero ningun parametro o region donde la “randi()” puede sacar una numero para la offset


La solucion para la problema es muy facil, solo necesitas despues del “randi()” % y una numero como “randi() % 2”. Despues de hacer eso la randi() puede sacar una numero aleatorio entre 0 y su numero maxima (2, 3, etc).
Potential Solution/ Solucion Potencial

$enemigos/Posicion.set_offset(randi() % 3)

I don’t know if this will be the answer for you but it might help.
Yo no sabo si esta solucion voy a ayudar pero sí puede si esto es la problema entre su programa.

thanks, that was the problem, but I have another query, I will create another topic

lokigamer00 | 2020-01-03 20:10