This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

Is there a way to change what material an object is using, using a script

Hello

I am working on a 3D game and I was hoping to add a mechanic where the character would change color at the press of a button.
I am using GD Script and I can not find a way to change the material of an object mid game.
Is there a way to accomplish this

Thanks, Ace : )

Best Answers

  • cyberealitycybereality Posts: 928
    Accepted Answer

    You can get the material if you call this function on a mesh instance, you can do this on _ready() (make sure to define "var mat" at the top of the script so the rest of the functions can see it):

    mat = get_surface_material(0)
    

    Note that if your script is not directly on the mesh instance, you will need to use get_node("") to get it.

    Then to change the material properties you can use this to change the color to something random:

    mat.albedo_color = Color(randf(), randf(), randf())
    

    Hope that helps.

  • Accepted Answer

    Works, thanks. What would I replace randf with if I want a specific color.

Answers

  • cyberealitycybereality Posts: 928Moderator
    Accepted Answer

    You can get the material if you call this function on a mesh instance, you can do this on _ready() (make sure to define "var mat" at the top of the script so the rest of the functions can see it):

    mat = get_surface_material(0)
    

    Note that if your script is not directly on the mesh instance, you will need to use get_node("") to get it.

    Then to change the material properties you can use this to change the color to something random:

    mat.albedo_color = Color(randf(), randf(), randf())
    

    Hope that helps.

  • Ace The Dolphin GuyAce The Dolphin Guy Posts: 5Member
    Accepted Answer

    Works, thanks. What would I replace randf with if I want a specific color.

  • cyberealitycybereality Posts: 928Moderator

    Cool. Those are just red/green/blue values from 0.0 to 1.0.

    For example, pure red would be:

    Color(1.0, 0.0, 0.0)
    

    Or white would be:

    Color(1.0, 1.0, 1.0)
    
  • awesome. One last thing. You mentioned having to use get_node("") if the script is not directly on the mesh, but I don't understand how get_node works. My mesh is a child of a kinimatic body which is where my script is.
    Does the get_node function allow me to use get_surface_material

  • cyberealitycybereality Posts: 928Moderator

    get_node() uses a format similar to navigating on the command-line in a console. If you wanted to get a direct child of the current node you would pass in the node's name like:

    get_node("MyMesh")
    

    Where "MyMesh" is the name of your mesh instance.

    Of if there were 2 levels (for example, the script was on a higher node) you can do this:

    get_node("MyBody/MyMesh")
    

    See here for more details:
    https://docs.godotengine.org/en/3.1/classes/class_node.html#class-node-method-get-node

  • func _ready():
        get_node("Player Mesh")
        mat = get_surface_material(0)
    

    So I got the mesh node, but it still gives me this error

    Method 'get_surface_material' is not declared in the current class.

    Am I missing somthing get get_surface_material into the class?

  • cyberealitycybereality Posts: 928Moderator
    edited December 2019

    get_surface_material is a member function. Which means you have to call it on an instance of the class.

    For example, something like this would work (assuming "Player Mesh" is a mesh instance):

    func _ready():
        var player_node = get_node("Player Mesh")
        mat = player_node.get_surface_material(0)
    
  • Ok, so I have to spesify the mesh mode when using get_surface_material. Got It

    Thank you for your help : )

  • cyberealitycybereality Posts: 928Moderator

    Sweet! So glad I could help.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.