Changing from "Screen Space - Camera" to "World Space" via script on startup?

I have been trying to work out how one would make a World Space UI scale to the camera size (screen resolution) for a few hours now, and one of the ideas that was floated was momentarily going to Screen Space - Camera to get the camera resolution, then switching to World Space.

I have already attempted this via an animation but the variable isn’t picked up, is there a way to do this in script, or a better way to get World Space to scale to the camera?

Get a reference to the canvas component on your canvas. Then set the renderMode of the canvas to RenderMode.WorldSpace.

public Canvas m_canvas;

void Start () {
     ChangeRenderMode (RenderMode.WorldSpace);
}

private void ChangeRenderMode (RenderMode m) {
     m_canvas.renderMode = m;
}

@KrayZLogic Thanks so much, but I don’t suppose this can be done (or you know how to do) this in Javascript?
I have most of my experience in JS and not much in C# yet.

Sorry to resurrect an old thread, but I am having a little trouble doing this in Javascript.

The error I am getting is BCE0019: ‘ChangeRenderMode’ is not a member of ‘UnityEngine.Canvas’.

var UICanvas : Canvas;
UICanvas.ChangeRenderMode(RenderMode.WorldSpace);

You are missing the function to change the render mode.

var m_canvas : Canvas;

function Start () {

    //Change the render mode without using a function
    m_canvas.renderMode = RenderMode.WorldSpace;
  
    //Use a function to change the render mode
    ChangeRenderMode (RenderMode.WorldSpace);

}

function ChangeRenderMode (RenderMode r) {
  
    m_canvas.renderMode = r;

}