Issue with sending information between two scripts

Hey everyone.

I have a script that detects when a player has fallen onto the floor which works fine, and the other script which I want to take that information to then end the game and switch to the results were the players can restart the match, go into the option, or exit the game/main menu.

The script that detects the player(s) is attached a GameObject while the other script is part of a simple state machine and isn’t attached anything.

Because I am new to all of this I am having trouble figuring how to send that information from one script to the other.

This is the script that detects

using UnityEngine;
using System.Collections;

public class PlayerDetection : MonoBehaviour {

	public void OnCollisionEnter(Collision col)
	{
		//TEST!!! Only upper body box collider is in use for the current test. 
		//Must inclued both colliders on each leg and arms, along with the head and lower body.

		if(col.gameObject.name == "bn_spineIK_d001")
		{

			Debug.Log("Player has touched the floor, one point to other player. GAME OVER!");
		}
	}

	public void Update ()
	{

	}
}

And this is the state script.

using UnityEngine;
using Assets.Code.Interface;

namespace Assets.Code.States
{
	public class PlayStateScene : IStateBase
	{
		private StateManager manager;
		PlayerDetection playerOnFloor;

		void Start()
		{
			playerOnFloor = GameObject.Find("RoomMesh001").GetComponent<PlayerDetection>();
		}

		public PlayStateScene (StateManager managerRef)
		{
			manager = managerRef;
			if (Application.loadedLevelName != "Scene 2 Gameplay")
				Application.LoadLevel ("Scene 2 Gameplay");
		}

		public void StateUpdate()
		{
			if (playerOnFloor)
			{
				manager.SwitchState (new ResultStateScene (manager));
			}
		}

		public void ShowIt()
		{
			Debug.Log ("In PlayStateScene1");
		}
	}
}

I could really do with some help of were to go next. Cheers in advance.

There’s several ways to go about this, but for the setup you’ve got, the easiest would be to just make the PlayerStateScene a singleton, and have it accessible from wherever:

public class PlayStateScene : IStateBase
{
    private static PlayStateScene _instance;
    public static PlayStateScene instance {
        get {
            return _instance;
        }
    }

    void Start()
    {
        //assume you only make one. You could make a check
        //and throw an error if _instance's already bound.
        _instance = this;
    }

Then you can simply pass the message along in the collision detection script:

public void OnCollisionEnter(Collision col)
{
    if(col.gameObject.name == "bn_spineIK_d001")
    {
        //Send the message to the instance.
        PlayStateScene.instance.RegisterCollision();
    }
}

You could also create a event/listener system where stuff like the PlayerDetection script registered themselves at some central event listener, but a singleton is quick and gets the job done. It’s pretty easy to debug too.

If you want to get fancy, don’t instantiate the singleton before it’s accessed the first time. Saves you the trouble of thinking about Start/Awake orders and whatnot, and a couple of lines of boring initialization code.

private static PlayStateScene _instance;
public static PlayStateScene instance {
    get {
        if(_instance == null)
            _instance = new PlayStateScene();
        return _instance;
    }
}