Dropdown menu determines in-game trigger

In my game, I’ve started off with 3 kinds of triggers that react to the player entering them : one is for rooms where they shouldn’t be caught by AI, the next is for outdoors areas, and the last makes everything silent. (Baldi’s Basics mod : faculty, playground, library) I’ve decided to put all these instructions in one script, so while triggers would use the same script, their behavior would depend on the selected option from the dropdown menu. I’ve managed to get the menu to appear but actually telling the script what happens when an option is chosen for a trigger, errors appear. Here’s the script :

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEditor.UIElements;
using UnityEngine.UIElements;

public class MultiPurposeTriggerScript : MonoBehaviour
{
	private void Start()
	{
		this.hitBox = base.GetComponent<BoxCollider>();
	}

	public enum triggerType
	{
		Faculty,
		Outdoors,
		Library
	}

	void Update()
	{
		if (this.ps.touchingGrass)
		{
			int num = Mathf.RoundToInt(UnityEngine.Random.Range(0f, 2f));
			this.gc.audioDevice.PlayOneShot(this.ambienceClips[num]);
			this.gc.audioDevice.loop = true; //for playing a random ambience
		}
	}

	private void OnTriggerEnter(Collider other)
	{
		if (other.gameObject.CompareTag("Player"))
		{
			if(this.triggerType = 1)
			{
				this.ps.touchingGrass = true;
			}
		}
	}

	private void OnTriggerStay(Collider other)
	{
		if (other.gameObject.CompareTag("Player"))
		{
			if(this.triggerType = 1)
			{
				this.ps.touchingGrass = true;
			}
		}
	}

	private void OnTriggerExit(Collider other)
	{
		if (other.gameObject.CompareTag("Player"))
		{
			if(this.triggerType = 1)
			{
				this.ps.touchingGrass = false;
			}
		}
	}

	public GameControllerScript gc;
	public PlayerScript ps;

	public triggerType Trigger;

	private BoxCollider hitBox;

	//Make the principal call-out 4th wall break, or control glitched door sound
	public AudioSource AffectedAudioSource;

	public AudioClip[] ambienceClips;
}

I’ve also tried writing “this.Trigger = 1” but that still didn’t work.

Errors have reasons. Go fix your errors.

If it is a nullref: The answer is always the same… 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

For ALL errors:

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

My apologies for not specifying.

No nullrefs as far as I remember, after some retrials my errors were “cannot reference a type through an expression” and “‘MultiPurposeTriggerScript.triggerType’ is a type but is used like a variable”.

*I say “retrials” because I’ve been fiddling around with other combinations of code that seemed like a possible workaround for the errors, before and after doing other things.
They involved stuff like changing between triggerType and Trigger; = or ==; Outdoors or 1…

Finding out how to fix errors is kind of one of the reasons Unity discussions pages and user manuals exist, you know that, right?
Sometimes people are willing to waste a bit of time to help out a developer, whether it’s their job or not.

It’s not an error that can just be “fixed”. You gotta know how the dropdown menu and enum stuff in question works. I’ve got a lot to learn on that subject :

Do I put ‘= Outdoors’ to say how my setting works? Nope! Outdoors “does not exist in the current context” even though it’s listed in the dropdown without quotes.
Do I replace “Outdoors” with “1” (as it’s the second on a list where the first is the “zeroth”)? Nope! Doesn’t change.
Do I put two equals instead of one before my setting? Nope! Doesn’t fix it either way.
Do I put () at the end of “public enum triggerType”? Nope! Spawns many more errors that extend into the whole script.
Do I replace “triggerType” with “Trigger”, which is a name that refers to the dropdown…
with the number referring to the setting? Nope! “Cannot implicitly convert type ‘int’ to ‘MultiPurposeTriggerScript.triggerType’.”
with the setting name? Nope! Still “does not exist in the current context”.

So yeah, any clear and helpful assistance that can help me add function to an Inspector dropdown’s setting would be greatly appreciated.
For now though, the only way I can fix my errors is by turning the whole instruction into lines of //quotes and by temporarily dealing with separate trigger scripts.

You’re asking basic C# language questions here, nothing to do with Unity.

The forums are not for teaching C# language ab initio. Pointed questions about particular stuff, yeah, as long as you use correct terminology, or at least attempt to… but not this:

Screen Shot 2024-09-14 at 3.59.52 PM

I’m sorry if you were somehow led to believe the above approach is how to learn something like software engineering. Alas, it is most certainly NOT the correct approach to learn C# programming.

I suggest working through some basic C# programming tutorials first. Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

Imphenzia: How Did I Learn To Make Games:

Okay, well thanks for the tips. I’ll just continue the research.

Note to self : found a video with some code that appears to work. May edit this reply if unsuccessful.

[SerializeField]
public triggerType Trigger;

and

if(Trigger == triggerType.Faculty)

conclusion : reference to enum == original enum.selected setting

video link : https://www.youtube.com/watch?v=hHoeDmxOx2M