I am new to using udon and I am trying to make a script that allows a light to turn on when I run through a box collider but I don’t know how to set up the code properly.
this was one idea that I had but nothing happened when i tested it
so I tried doing something like this but it didn’t work the way I wanted it too. It would only turn the light on when I had the object hitting something( like the wall or the floor) instead of activating when I go through the collider. pls help.
(Flagpole2 is the object that the player is holding, and point light is the light source that I need to activate)
Do you only want the light to turn on if the flag is being held? Should the light turn back off if the player drops the flag even if both are still in the collider?
I want the light to turn on when I bring the flag inside a box collider and turn off when the flag leaves the collider, and it’s only for the one flag.
Ok, um, I’m not certain how to do it with the udon nodes, but I can write it up in udonsharp. I suppose I can try to translate it if using udonsharp is not an option.
The light gameobject needs a collider and udon behavior to hold the following code.
when the flagpole gameobject enters the light’s collider it will toggle the Light component on and off. (also you probably want to turn the light component off by default)
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
public class FlagpoleLightToggleTest : UdonSharpBehaviour{
public GameObject Flagpole;
private void OnTriggerEnter(Collider other) {
if (other.gameObject == Flagpole) {
gameObject.GetComponent<Light>().enabled = true;
Debug.Log("Light turned on");
}
}
private void OnTriggerExit(Collider other) {
if (other.gameObject == Flagpole) {
gameObject.GetComponent<Light>().enabled = false;
Debug.Log("Light turned off");
}
}
}
If it’s not to much trouble for you to try and translate it, I would appreciate it, but I think that I can try and put it together myself in the graph if you’re not able too.
When I bring the flagpole over to the collider, the console gives me this error:
[<color=yellow>UdonBehaviour] An exception occurred during Udon execution, this UdonBehaviour will be halted.
VRC.Udon.VM.UdonVMException: The VM encountered an error!
Exception Message:
An exception occurred during EXTERN to ‘UnityEngineLight.__set_enabled__SystemBoolean__SystemVoid’.
Parameter Addresses: 0x00000005, 0x00000006
Object reference not set to an instance of an object
Inner Exception:
—> VRC.Udon.VM.UdonVMException: An exception occurred during EXTERN to ‘UnityEngineLight.__set_enabled__SystemBoolean__SystemVoid’.
Parameter Addresses: 0x00000005, 0x00000006
—> System.NullReferenceException: Object reference not set to an instance of an object
at VRC.Udon.Wrapper.Modules.ExternUnityEngineLight.__set_enabled__SystemBoolean__SystemVoid (VRC.Udon.Common.Interfaces.IUdonHeap heap, System.Span1[T] parameterAddresses) [0x0002d] in <1755b686cafc4fc386605c6273c709b3>:0 at VRC.Udon.VM.UdonVM.Interpret () [0x00273] in <723aa2dc745945758d5f43cdd4ea19ca>:0 --- End of inner exception stack trace --- at VRC.Udon.VM.UdonVM.Interpret () [0x0033c] in <723aa2dc745945758d5f43cdd4ea19ca>:0 --- End of inner exception stack trace --- at VRC.Udon.VM.UdonVM.Interpret () [0x00436] in <723aa2dc745945758d5f43cdd4ea19ca>:0 at VRC.Udon.UdonBehaviour.RunProgram (System.UInt32 entryPoint) [0x0006d] in D:\unity\My project (1)\Assets\Udon\UdonBehaviour.cs:979 UnityEngine.Debug:LogError (object,UnityEngine.Object) VRC.Core.Logger:LogError (string,int,UnityEngine.Object) VRC.Udon.UdonBehaviour:RunProgram (uint) (at Assets/Udon/UdonBehaviour.cs:993) VRC.Udon.UdonBehaviour:RunEvent (string,System.ValueTuple2<string, object>) (at Assets/Udon/UdonBehaviour.cs:1182)
VRC.Udon.UdonBehaviour:OnTriggerEnter (UnityEngine.Collider) (at Assets/Udon/UdonBehaviour.cs:798)
[<color=yellow>UdonBehaviour] An exception occurred during Udon execution, this UdonBehaviour will be halted.
VRC.Udon.VM.UdonVMException: The VM encountered an error!
Exception Message:
An exception occurred during EXTERN to ‘UnityEngineLight.__set_enabled__SystemBoolean__SystemVoid’.
Parameter Addresses: 0x00000005, 0x00000006
Object reference not set to an instance of an object
Inner Exception:
—> VRC.Udon.VM.UdonVMException: An exception occurred during EXTERN to ‘UnityEngineLight.__set_enabled__SystemBoolean__SystemVoid’.
Parameter Addresses: 0x00000005, 0x00000006
—> System.NullReferenceException: Object reference not set to an instance of an object
at VRC.Udon.Wrapper.Modules.ExternUnityEngineLight.__set_enabled__SystemBoolean__SystemVoid (VRC.Udon.Common.Interfaces.IUdonHeap heap, System.Span1[T] parameterAddresses) [0x0002d] in <1755b686cafc4fc386605c6273c709b3>:0 at VRC.Udon.VM.UdonVM.Interpret () [0x00273] in <723aa2dc745945758d5f43cdd4ea19ca>:0 --- End of inner exception stack trace --- at VRC.Udon.VM.UdonVM.Interpret () [0x0033c] in <723aa2dc745945758d5f43cdd4ea19ca>:0 --- End of inner exception stack trace --- at VRC.Udon.VM.UdonVM.Interpret () [0x00436] in <723aa2dc745945758d5f43cdd4ea19ca>:0 at VRC.Udon.UdonBehaviour.RunProgram (System.UInt32 entryPoint) [0x0006d] in D:\unity\My project (1)\Assets\Udon\UdonBehaviour.cs:979 UnityEngine.Debug:LogError (object,UnityEngine.Object) VRC.Core.Logger:LogError (string,int,UnityEngine.Object) VRC.Udon.UdonBehaviour:RunProgram (uint) (at Assets/Udon/UdonBehaviour.cs:993) VRC.Udon.UdonBehaviour:RunEvent (string,System.ValueTuple2<string, object>) (at Assets/Udon/UdonBehaviour.cs:1182)
VRC.Udon.UdonBehaviour:OnTriggerEnter (UnityEngine.Collider) (at Assets/Udon/UdonBehaviour.cs:798)
If it’s not to much trouble for you to try and translate it, I would appreciate it, but I think that I can try and put it together myself in the graph if you’re not able too.