Triggers and Other Game Objects

I have been programming for over 15 years and things seemed pretty straight forward to start with in Unity, but I have run into a learning curve snag. I am attempting to put together a short C# script that will answer many questions for me as to how to properly code these projects and have a two part question.

As a simple example I will have 2 objects, a point light and a cube. The cube will be invisible or not, that part is irrelevant. When the player walks through the cube the light will come on.

The script should be straight forward, changing the intensity of the light from 0 to any larger number. Here’s my issues. I assume that the cube should be a “trigger” and the script is attached to the cube, not the light. I both named the light and gave it a tag. It seemed that reference wants me to use “FindWithTag” to create a reference to the light. I do that just fine with no errors, but when adding the Instantiate line to the script I get an ““Only assignment, call, increment, decrement, and new object expressions can be used as a statement”” error. I assume that the line
public GameObject myitem=GameObject.FindWithTag("Finish");
would inherit all the attributes of the original object(I used the tag “Finish” for the light, just to be quick). However, if I then try something like thelight.intensity=100;
I get an error stating that there is no
reference “intensity.”

The whole thing is a little confusing to me and the documentation is put together in an odd sort of way.

The second part of the question is simply whether I need to call an onCollision or not, it would seem that calling that function would simply be redundant. Thanks in advance. i choose to work on this short example because it will literally answer about a dozen questions for a person like me and open the whole deal up for me.

I ended up figuring this one out after a bit of work. For anyone else that is having the same issue I am giving the short version. You can use the “Find” or “FindWithTag” to find a GameObject. Let’s say in my above example that the line I used to find it is…


you can then use a second reference to access the light portion of that GameObject as in…


From there you can refer to mylight as a light and make appropriate changes.

  1. You should post your code so we can better understand what you’re saying.
  2. You can Find by name too. The object returned is a GameObject. GameObjects can have Components attached to them. In this case, you have a Light attached to that game object. To access it, something like GameObject light = GameObject.FindWithTag(“Finish”).GetComponent(Light).intensity = 1f;
  3. Set the box collider to ‘is trigger’ and you’ll get OnCollisionEnter and related methods called when something enters the box collider. You can use that to turn the light on/off.
  4. You can turn the light on/off by setting the ‘active’ flag on the game object, or ‘enabled’ flag on the light Component
  5. You can have your script have a GameObject var, and assign the light to that, by drag/drop in the Inspector, thus avoiding the need for Find or FindWithTag

Hope this helps

You can only use simple expressions to initialize a public variable, not UnityEngine functions like FindXXX or GetComponent. You should search the object at Start:

public GameObject myitem;

void Start(){
  myitem = GameObject.FindWithTag("Finish");

In this case, myitem is a GameObject reference. GameObject is the basic object to which specific components are added. When you create a Light, for instance, actually a GameObject is created and a Light component is added to it. You must use the property light to access Light stuff, like this:

  GameObject theLight;
  theLight.light.intensity = 0;

Finally: if the collider is a trigger, only OnTrigger events will be generated. If it’s a regular collider, only OnCollision events will happen.

I will have to look into the last part about drag and drop along with a var. I ended up with a very simple code…

using UnityEngine;
using System.Collections;

public class mytest : MonoBehaviour {

GameObject theholder;
Light mylight;	

void OnTriggerEnter()


This was not really something that I needed to do, just an example that helped me understand the basic referencing setup in Unity. For this point I can now go a lot further now that I see how the relationships work, thanks for the answers.

Hi Everyone!!

Happy to have found this thread - thank you!! - it has helped me out tremendously.

I do have one question, however, with the script. Perhaps this is more of a C# question, but I thought I would ask it in the context of this thread.

I can write the script in the following two ways, and the outcome is the same:

GameObject theholder;
Light mylight;

GameObject theholder;



In the 1st script, I’m confused what “Light mylight;” actually does? Am I adding a “Light” component to my referenced “myLight”? If so - this seems redundant compared to the second script. And is there any reason why I would prefer one over the other?

Thank you so much in advance - I’m also learning how Unity/C# referencing works.