I don’t believe Character
is a class in Unity. You might be wanting CharacterController
. Although the constructor for this isnt exposed to Udon.
If Character
is a custom class that you have written, I dont believe Udon can do this, instead you will need to follow my instructions below but with your custom class. Note that if you want to create multiple objects at runtime that are synced across the network, you will need to use object pools.
There are many C# classes, methods and properties that sadly arent accessible from udon at this point, which is kinna sad. From my experience it doesn’t seem like most constructors are exposed. Ive ran into it many times working on maps, its super annoying :<
If you go to the top menu VRChat SDK -> Udon Sharp -> Class Exposure Tree
you can see all things accessible/exposed to udon. With the latest VRC SDK, it doesn’t seem new CharacterController()
is exposed :(
Fetching from current GameObject
If CharacterController
is what you are looking for, then instead of creating one at runtime which you cant do, instead you could try create it in the editor, and get the existing one at runtime.
On the GameObject with your script, add a CharacterController
component. Then in your code you can do something like this
CharacterController chara = GetComponent<CharacterController>();
Creating new from instanced prefab
Or if you really need to create a CharacterController
at runtime (which I highly discourage due to the fact that there is no network syncing). You could instead create a new GameObject with just the character component added, and save it as a prefab by dragging it into the project tab.
Then in your script you can instantiate this prefab by using the following code:
public class CharacterControllerInstancer : UdonSharpBehaviour {
//SET ME IN THE EDITOR TO THE PREFAB YOU CREATED
public GameObject characterPrefab;
public CharacterController New() {
GameObject instance = Instantiate(characterPrefab);
return instance.GetComponent<CharacterController>();
}
}
Ive found this approach to be useful if you are creating client side things like lists of UI items.