How do I access a variable from another script in C#?

Anderoo99Anderoo99 Posts: 74Member
edited September 4 in Programming

I've been trying to figure this out for more than a day.

The variable I am trying to access is public in the other file.

Here is the scene file. I am trying to access the script from Sprite_SpawnPoint to the script attached to Atom_Count

Here is the code

_public class Atom_Count : Label
{
    Sprite_SpawnPoint totalAtoms;

    public override void _Ready()
    {
        totalAtoms = GetTree().GetRoot().GetNode<Sprite_SpawnPoint>("Spawner/Sprite_SpawnPoint");
    }

    public override void _Process(float delta)
    {
        Text = String.Format("{0:0000}", totalAtoms.totalAtomsInShell);
    }
}_

Here are the errors shown

Thanks in advance!!! =)


Tags :

Best Answer

  • TwistedTwiglegTwistedTwigleg Posts: 1,721
    Accepted Answer

    Looking at the errors shown, it looks like the GetNode function calls is unable to find the node at the given path.
    I haven't really used C# in Godot very much, but this might fix it:

    totalAtoms = GetTree().GetRoot().GetNode<Sprite_SpawnPoint>("Level_Game/Spawner/Sprite_SpawnPoint");
    

    I think GetRoot returns the root of the tree, which is a Viewport Node, so you probably need to include Level_Game in the node path. You can view the scene tree from the remote inspector when running a Godot scene, which may help with figuring out what is going on.

    Hopefully this helps!

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 1,721Admin
    Accepted Answer

    Looking at the errors shown, it looks like the GetNode function calls is unable to find the node at the given path.
    I haven't really used C# in Godot very much, but this might fix it:

    totalAtoms = GetTree().GetRoot().GetNode<Sprite_SpawnPoint>("Level_Game/Spawner/Sprite_SpawnPoint");
    

    I think GetRoot returns the root of the tree, which is a Viewport Node, so you probably need to include Level_Game in the node path. You can view the scene tree from the remote inspector when running a Godot scene, which may help with figuring out what is going on.

    Hopefully this helps!

  • Anderoo99Anderoo99 Posts: 74Member

    @TwistedTwigleg said:
    Looking at the errors shown, it looks like the GetNode function calls is unable to find the node at the given path.
    I haven't really used C# in Godot very much, but this might fix it:

    totalAtoms = GetTree().GetRoot().GetNode("Level_Game/Spawner/Sprite_SpawnPoint");

    I think GetRoot returns the root of the tree, which is a Viewport Node, so you probably need to include Level_Game in the node path. You can view the scene tree from the remote inspector when running a Godot scene, which may help with figuring out what is going on.

    Hopefully this helps!

    Thanks, that solved the issue. =) =)

  • TwistedTwiglegTwistedTwigleg Posts: 1,721Admin
    edited September 5

    No problem! :smile:

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file