So I've been trying out Godot's C# and I cant seem to get past this error I'm not sure what I'm doing wrong. My C# is a bit rusty so it could be a simple error. Any help is appreciated.

here's the code:

using Godot;
using System;

public class Spatial : Godot.Spatial
{
    
    private Spatial node;
   

    private float rot = 0.1f;

    public override void _Ready()
    {
       node = GetNode("Spatial2") as Spatial;
      
    }

    public override void _PhysicsProcess(float delta)
    {
       

       node.RotateY(rot);
       
    }
}

Welcome to the forums @Cryptic_Dev!

What line does the error occur? Also, have you made sure there is a node called Spatial2 as a child of the node this is attached to?

@TwistedTwigleg I actually fixed the problem by using "(node as Spatial).RotateY(rot);" But I do need help with doing a random number generator in c#.

Great, I'm glad you figured it out.

For a random number generator, I would just use the C# Random class.

@TwistedTwigleg So I used the Random class and it causes the error "cannot instance script because the class 'class_name' could not be found"

Can you show me the code you are using? Also, I do not think you need class_name in C# since it’s a statically typed programming language.

@TwistedTwigleg

using Godot;
using System;

public class swat3 : Spatial
{
    private int num;
	private AnimationPlayer anim_player;
	private Timer timer;
	private Random rng;

	// Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        anim_player = GetNode("RootNode/AnimationPlayer") as AnimationPlayer;
		timer = GetNode("RootNode/Timer") as Timer;
        rng = new Random();
		//timer.Connect("timeout", this, "_on_Timer_timeout");
    }
    	public void _on_Timer_timeout()
	{
		num = rng.Next(1, 10);
		(timer as Timer).Start();
	}

    	public override void _PhysicsProcess(float delta)
	{
		
        
		if (num == 1){
			(anim_player as AnimationPlayer).Play("crouch to stand");
		}
		if (num == 2){
			(anim_player as AnimationPlayer).Play("Idle");
		}
		if (num == 3){
			(anim_player as AnimationPlayer).Play("mixamo.com");
		}
		if (num == 4){
			(anim_player as AnimationPlayer).Play("run");
		}
		if (num == 5){
			(anim_player as AnimationPlayer).Play("run_backwards");
		}
		if (num == 6){
			(anim_player as AnimationPlayer).Play("stand to crouch");
		}
		if (num == 7){
			(anim_player as AnimationPlayer).Play("strafe_left");
		}
		if (num == 8){
			(anim_player as AnimationPlayer).Play("strafe_right");
		}
		if (num == 9){
			(anim_player as AnimationPlayer).Play("walk");
		}
		if (num == 10){
			(anim_player as AnimationPlayer).Play("walk_backward");
		}
		
		}
}

Which line does Godot say is causing the issue? I don't see any class_name in the script, so it's hard to guess right off.

I did make a few minor changes to the script though, like setting num in _ready to avoid it being null in _physics_process. Here's the script:

using Godot;
using System;

public class swat3 : Spatial
{
    private int num;
	private AnimationPlayer anim_player;
	private Timer timer;
	private Random rng;

	// Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        anim_player = GetNode("RootNode/AnimationPlayer") as AnimationPlayer;
		timer = GetNode("RootNode/Timer") as Timer;
        rng = new Random();
		//timer.Connect("timeout", this, "_on_Timer_timeout");
	// new - set num so it's not null on the first _physics_process call
	num = rng.Next(1, 10);
    }
    	public void _on_Timer_timeout()
	{
		num = rng.Next(1, 10);
		(timer as Timer).Start();
	}
    	public override void _PhysicsProcess(float delta)
	{
		// new - a tiny optimization, but with this it means we won't be trying to change the animation every _physics_process frame, and instead will only change it once the animation as finished. May or may not be needed, but it will prevent the code for repeatedly calling the play function every physics tick (30 times a second)
		if (anim_player as AnimationPlayer).IsPlaying() == false) {
			if (num == 1){
				(anim_player as AnimationPlayer).Play("crouch to stand");
			}
			if (num == 2){
				(anim_player as AnimationPlayer).Play("Idle");
			}
			if (num == 3){
				(anim_player as AnimationPlayer).Play("mixamo.com");
			}
			if (num == 4){
				(anim_player as AnimationPlayer).Play("run");
			}
			if (num == 5){
				(anim_player as AnimationPlayer).Play("run_backwards");
			}
			if (num == 6){
				(anim_player as AnimationPlayer).Play("stand to crouch");
			}
			if (num == 7){
				(anim_player as AnimationPlayer).Play("strafe_left");
			}
			if (num == 8){
				(anim_player as AnimationPlayer).Play("strafe_right");
			}
			if (num == 9){
				(anim_player as AnimationPlayer).Play("walk");
			}
			if (num == 10){
				(anim_player as AnimationPlayer).Play("walk_backward");
			}
		}
		
		}
}

@TwistedTwigleg godot doesnt tell me which line is causing the problem but im pretty sure the line " private Random rng;" is causing the problem because after I commented out the line and every thing was fine

also the godot error:

E 0:00:11.064 can_instance: Cannot instance script because the class 'swat3' could not be found. Script: 'res://swat3.cs'. <C++ Error> Method failed. Returning: __null <C++ Source> modules/mono/csharp_script.cpp:2905 @ can_instance()

a year later