Trying to activate a light turn on when holding specific object in a certain spot

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

Capture2
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?

How many flagpoles are there?

How many lights are there?

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");
        }
    }
}

image

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.

This seems to work.

it didn’t work

flagpole

Hope this helps.

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

Program Counter was at: 184

Stack Dump:

Heap Dump:
0x00000000: True
0x00000001: flagpole 2 (UnityEngine.GameObject)
0x00000002: flagpole 2 (UnityEngine.GameObject)
0x00000003: flagpole 2 (UnityEngine.CapsuleCollider)
0x00000004: flagpole 2 (UnityEngine.CapsuleCollider)
0x00000005: null
0x00000006: True
0x00000007: Point Light (UnityEngine.GameObject)
0x00000008: light
0x00000009: Point Light (UnityEngine.GameObject)
0x0000000A: False
0x0000000B: Point Light (UnityEngine.GameObject)
0x0000000C: null
0x0000000D: null
0x0000000E: null
0x0000000F: null
0x00000010: False
0x00000011: Point Light (UnityEngine.GameObject)
0x00000012: light
0x00000013: Point Light (UnityEngine.GameObject)
0x00000014: flagpole 2 (UnityEngine.GameObject)
0x00000015: UnityEngineCollider.__get_gameObject__UnityEngineGameObject
0x00000016: UnityEngineGameObject.__Equals__SystemObject__SystemBoolean
0x00000017: UnityEngineGameObject.__get_gameObject__UnityEngineGameObject
0x00000018: UnityEngineGameObject.__GetComponent__SystemString__UnityEngineComponent
0x00000019: UnityEngineLight.__set_enabled__SystemBoolean__SystemVoid

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)

It just gave 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

Program Counter was at: 184

Stack Dump:

Heap Dump:
0x00000000: True
0x00000001: flagpole 2 (UnityEngine.GameObject)
0x00000002: flagpole 2 (UnityEngine.GameObject)
0x00000003: flagpole 2 (UnityEngine.CapsuleCollider)
0x00000004: flagpole 2 (UnityEngine.CapsuleCollider)
0x00000005: null
0x00000006: True
0x00000007: Point Light (UnityEngine.GameObject)
0x00000008: light
0x00000009: Point Light (UnityEngine.GameObject)
0x0000000A: False
0x0000000B: Point Light (UnityEngine.GameObject)
0x0000000C: null
0x0000000D: null
0x0000000E: null
0x0000000F: null
0x00000010: False
0x00000011: Point Light (UnityEngine.GameObject)
0x00000012: light
0x00000013: Point Light (UnityEngine.GameObject)
0x00000014: flagpole 2 (UnityEngine.GameObject)
0x00000015: UnityEngineCollider.__get_gameObject__UnityEngineGameObject
0x00000016: UnityEngineGameObject.__Equals__SystemObject__SystemBoolean
0x00000017: UnityEngineGameObject.__get_gameObject__UnityEngineGameObject
0x00000018: UnityEngineGameObject.__GetComponent__SystemString__UnityEngineComponent
0x00000019: UnityEngineLight.__set_enabled__SystemBoolean__SystemVoid

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)

when i tested it it ran into some sort of error

i got this error when the flag entered the collider

and this is the code that I tried from you

Did you drag your Flagpole object to the Flagpole variable on the light’s udon behavior?

image

it still gave me the error

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.