- Home /
extension for object
i created an extension for Object so i can use it freely.I did this
public static class extension {
// Use this for initialization
public static void test(this Object ob)
{
Debug.Log("it is done", ob);
}
after that when i want to call it it don't work
camera_main = Camera.main; Object.test();void Start() {
} so if extension doesn't work for object or i did wrong.
Comment
Try moving your extension script to other script. If I remember correctly, extensions can't be called in the same class (or file, I forgot)
What "does not work"? The code does not compile or you don't see anything in the console?
It works perfectly fine here if you call Camera.main.test()
Answer by kala-hari · Jul 15, 2019 at 09:37 AM
It' looks like you are trying to call test()
as a static method ( Object.test()
).
Does it work if you call it on an instance? You could call it on the instance you are implementing the Start()
method. E.g.:
void Start() {
camera_main = Camera.main;
this.test();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612204907im_/https://answers.unity.com/themes/thub/images/avi.jpg)