Trigger Script not functioning as expected

Attempting to create a series of moving trigger colliders that pass through a single collider which then reads the tag for use later in the script and plays a sound each time entered. I’m pretty new to this sort of thing so the code is anything but eloquent but it’s progress.

outside of update, no start declarations stated as necessary for functions

//VALUE WHEEL FUNCTIONS
    void OnTriggerEnter(Collider other) 
    {
        //check for trigger detection then the tag of the obj 
        //and play wheel sounds then use tag to fill text value for UI and calc       
        if (other.gameObject.tag == "0" || other.gameObject.tag == "1"|| other.gameObject.tag == "2" || other.gameObject.tag == "3" || other.gameObject.tag == "4" || other.gameObject.tag == "5" || other.gameObject.tag == "6" || other.gameObject.tag == "7" || other.gameObject.tag == "8" || other.gameObject.tag == "9") 
        {
            source.pitch = UnityEngine.Random.Range(1f, 2f);
            source.Play();
            //sets public variable string for value wheel value 
            //to the tag of the object it collided with
            WheelCalcValue = other.gameObject.tag;
        }
    }

couldn’t find a reason for the problem within doc, no errors presented on compile, just not functioning during tests. thanks in advance, I know it’s super jank

First you need to determine whether the callback is even happening, namely with Debug.Log. You can also use that to print out information on the objects you’re colliding with.

For example:

void OnTriggerEnter(Collider other) 
{
    Debug.Log($"{name} collided with {other.name} that has tag {other.gameObject.tag}", this);
}

From there you can start to debug the issue.

Though are your tags literally just 0, 1, 2, 3 etc? That doesn’t seem very descriptive.

I’ll run that see if it’s properly detecting collision. the reason the tags are 0-9 is so I can use the strings for UI during testing. it’s just a single slot machine wheel (with numbers 0-9) with box colliders aligned on the faces passing through a single trigger that reads the tags and displays them. using it alongside streamerbot for a multiplier wheel for my chat’s gamble command systems. pretty simple on paper, but this is my first project outside of tutorials.

It’s a component. Component.tag just returns the tag of its respective game object: UnityCsReference/Runtime/Export/Scripting/Component.bindings.cs at master · Unity-Technologies/UnityCsReference · GitHub

1 Like

okay, so it’s not detecting collision at all, not reading out in log. I’ll double check my collider setups. Any idea where to start?

collider with script:

The docs tell you all the requirements for getting OnTriggerEnter to work: Unity - Scripting API: Collider.OnTriggerEnter(Collider)

Mind you, I would not be using collisions for this. If you’re spinning a roulette wheel, you can simply play the sound every x degrees or radians.

Same for the results as well. If you maintain your own rotation, then you can deduce what the results are via simple math.

would that use local rotation values? the parent will be rotating/pivoting using sine/cosine function.

also, I saw that this would use rigidbody, so that’s likely not an option given I dont want actual collision physics. will likely have to rework this to use rotation like you said.

No you don’t use the transform’s rotation, you manage your own rotation value, generate a rotation out of said value, then apply that to the slot. Quaternion.AngleAxis will be key here.

You can make the rigidbody kinematic, so it doesn’t respond to physics, but has collision.

didn’t even think about kinematic, that should work out perfectly for the time being.

Quaternion sounds scary but I’m sure that would run and scale better if I ever wanted to add random elements. I’ll likely end up swapping to that once I can figure out how to script that properly, but that’s another couple days of design away at my pace lol

thanks for all the help, I’ll check if rigidbody kinematic resolves the issue real quick

1 Like

alright, so one last issue. I’m getting NullReferenceExeption : Object reference not set to an instance of an object. and that seems to be pointing to the source.pitch . the audio source is setup as a component of the trigger and is setup earlier in the project via:

private AudioSource source ;
private AudioClip clip ;

You probably just have one of the components where you forgot to set it up.

It’s always the same three steps… always!!

How to fix a NullReferenceException error

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

NullReference is the single most common error while programming. Fixing it is always the same.

Some notes on how to fix a NullReferenceException error in Unity3D:

http://plbm.com/?p=221