How to switch between viewporttextures

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

Hello everyone,

I am trying to make a CCTV system for my game.
I have a few camera’s placed in scene and a monitor to show what the camera’s are seeing. I use a ViewportTexture for the monitor to show what the camera’s are seeing.
My idea is that when you click on the monitor it switches to another camera.
The problem is that when I link the texture to the viewport in the editor, it works fine. But when I try do change the ViewportTexture by using a script, nothing happens.

This is an example that doesn’t work for me (The file Cam1.tres is linked to the first camera and it’s viewport):

onready var Cam1 = preload("res://Cam1.tres")

func _ready():
    material = $Monitor.get_surface_material(0)
    material.albedo_texture = Cam1
    material.resource_local_to_scene = true
    $Monitor.set_surface_material(0, material)

I have read that for it to work, the material and the texture must have local to scene set to true.
Also, when I run the game and look at the screen in the editor, it shows me there isn’t a material applied.

Does anyone know how to fix this? (or know how to change the viewport textures by script)