- Home /
Accessing a variable in one script from another script? (using C#)
Okay, I've been trying to figure this out forever but it's not working and it's driving me crazy.
I've got two game objects, and I need the second game object to do something when the player collides with the first game object.
The first game object, which works exactly like it's supposed to, is just a red cube that I've set up to detect collisions from the player using the following script:
collision.cs
using UnityEngine; using System.Collections;
public class collision : MonoBehaviour { public static bool touchedRedCube = false; int i = 0;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
i++;
Debug.Log("Touched the cube " + i);
touchedRedCube = true;
}
}
}
Now like I said, that above code works just fine. The problem comes in when I try to have another game object preform an action when the player collides with the red cube. This other game object is just an EmptyGameObject with the following script attached:
test.cs
using UnityEngine; using System.Collections;
public class test : MonoBehaviour {
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (collision.touchedRedCube == true);
{
Debug.Log("Preformed an action.");
}
}
}
However, instead of printing "Preformed an action" to the debug log only when the player touches the red cube like it should, the above script starts printing the message as soon as the level loads, and continually prints it every single frame. And no, the player is not spawning directly on top of the red cube. I specifically set the scene up with the cube positioned a little ways in front the player so you have to walk to touch it.
What am I doing wrong?
the code seems ok, the only thing that maybe is not working is the OnTriggerEnter, you can try replacing this by void OnCollisionEnter(Collision collisionInfo) or a mouse option with the respective validations, also you can put the code on the update and make a raycast for the collision
Answer by Jessy · Feb 11, 2011 at 07:19 AM
http://unity3d.com/support/documentation/ScriptReference/index.Writing_Scripts_in_Csharp.html
Use the Awake or Start function to do initialisation.
What you would put outside any functions in Javascript, you put inside Awake or Start function in C# or Boo.
Static variables are especially a pain if you don't listen to that advice.
Personally, I use setup functions triggered by menu items, in the Editor, instead of Awake or Start, but, same idea.
Edit: collision is a bad name for a class, for three reasons. Class names should start with a capital letter, Unity already has a class named Collision, and it is not very descriptive. Rename it what you like, but not collision.
using UnityEngine;
public class DescriptiveName : MonoBehaviour {
public static bool touchedRedCube;
public void Awake () { touchedRedCube = false; }
int i; void OnTriggerEnter (Collider other) { if (other.CompareTag("Player") {
i++; Debug.Log("Touched the cube " + i); touchedRedCube = true; } }
}
.
using UnityEngine;
public class Test : MonoBehaviour {
void Update () { if (DescriptiveName.touchedRedCube) Debug.Log("Performed an action."); }
}
I'm very new to coding. Do you have perhaps an example of how to set that up?
I edited it to use Awake(). Explore this page if you want to get an idea of how I do it, and then ask another question if you need more relevant information. http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$enuItem.html