My Code isn't appearing

Godot Version

Godot 4.3

Question

Hello all, I am currently trying to code for a game and one of the things in the game is changing the texture of a sprite when the player opens the game. However, It seems that when I start the scene, nothing happens. Is there any reason for this?


The code

The result ,it’s supposed to show a fish.
I’m not really sure why this is happening.

randf_range creates a random float within a certain range. You compare that float to integer values. It’s technically not impossible to luck out and roll an exact ‘2’ with that function, but it’s very unlikely.

Use randi_range instead to generate a random integer within a certain range.

1 Like

You’re using $Corydora.texture = load(…) but your script appears to be attached to the Corydora node itself. When you use $Corydora, you’re trying to find a child node named “Corydora” under itself, which doesn’t exist

func _ready() -> void:
    var rng = RandomNumberGenerator.new()
    var version = rng.randi_range(0, 3)
    print(version)
    
    if(version == 0):
        texture = load("res://Assets/1.png")
    elif(version == 1):
        texture = load("res://Assets/2.png")
    elif(version == 2):
        texture = load("res://Assets/3.png")
    elif(version == 3):
        texture = load("res://Assets/4.png")

2 Likes

Hey I caught that too haha, I just corrected it without thinking lol.

3 Likes