Collision on other object!

So listen to this:

I am trying to make a Five switches puzzle. The player has to jump on 5 different stones in the right order to get an object. But I dont want to make 5 different scripts for each switch. I just want to make 1 script wich controls the other 5 switches.

Is there a way to use the OnCollisionEnter function on other objects than the object wich got the script added?

Here's my interpretation of your question.

If you want to have a logic with one script as this sample tries to show:

switch_1_Object --> SwitchScript switch_2_Object --> SwitchScript switch_3_Object --> SwitchScript switch_4_Object --> SwitchScript switch_5_Object --> SwitchScript

Then like all other logic you need a controller, something that can compare states events and or changes to them (knows what's going with all). Currently there is no such thing. You want to regard each script as the controller?

Did I understand your question correctly? If not can you please explain? And what language are you using C# or JS?

And collision functions can be used, you might even be able to use one twice in the same class, even though there is absolutely no functional reason to ever do so.

From what I gather, you want a Switch that has an ID or Tag value namely "blue" "red" "yellow" "purple" "green" which u can bump as message to a controller object which can keep track of changes in state

Say the switches are in this order

blue --> yellow --> red --> green --> purple 
0    -->      0 -->   0 -->  1 -->      0

Player jumped on green in above sample and controller receives a change in a [Flags] enum for example (.NET 4.0 compilation though, there are other solutions then FlagsAttributes) Controller will simply have to loop through a local var updated with the changes to accept input as blue only. When blue is true, it will allow writing to only yellow , if otherwise all values are back to zero (and maybe an added response to reset player location? depending on the game)

Anyway help me understand the question better and I'm sure I can help out :)

EDIT 11/19/2010 13:15:

Ok so if we build something that has 1 script for all the senders ( blocks you can jump on in this case ) and a Tag we should have enough for a simple sender script.

My knowledge of JS is poor at best , all I can do with it is convert it to C#. So I will try to be as pseudo as possible. The rest I unfortunatly can only hope to help with C#

SenderScript

public class SenderScript : MonoBehaviour 
{    
    public int ID;

    void Start()
    {
        if (ID == null) //If the ID has nothing assigned to it from the editor
        {
            Debug.LogError("Sender unable to send! No value was assigned");
        }
    }

    void Update()
    {

    }

    //No idea if JS treats the collision params the same way as C#
    void OnCollisionEnter(/*params*/)
    {
        //I think for JS you need SendMessage or such things
        //Hopefully this script [reference][1] is the message u need to let the controller
        //know what kind of block we are dealing with    
    }

} //end class

The controller

public class ControllerScript : MonoBehaviour
{
    private bool CorrectOrder;
    private int previousValue;    
    private bool fullSequence;

    private bool puzzleComplete;    

    void Start()
    {
        previousValue = 0;
        puzzleComplete = false;
    }

    void Update()
    {
    //I am guessing the receiver with sendmessage may get some other params with the
    //existing send message feature already having them
    //This should just get the message from the sender and extract the ID of the sender
    void ReceiveMessage(int ID)
    {
        // In C# I would build a switch here, see what the value is
        // For pseudo I would do something like this
        if (ID == 1)
        {
           if (CheckPrevious(1))
           {
               Order = true;
           }
           else
           {
               Order = false;
               previousValue = 0;
           }

           previousValue = 1;
        }
        if (ID == 2)
        {
           if (CheckPrevious(2))
           {
               Order = true;
           }
           else
           {
               Order = false;
               previousValue = 0;
           }
        }
        if (ID == 3)
        {
           if (CheckPrevious(3))
           {
               Order = true;
           }
           else
           {
               Order = false;
               previousValue = 0;
           }
        }
        if (ID == 4)
        {
           if (CheckPrevious(4))
           {
               Order = true;
               previousValue = 0;
           }
           else
           {
               Order = false;
               previousValue = 0;
           }
        }
        if (ID == 5)
        {
           if (CheckPrevious(5))
           {
               Order = true;
               fullSequence= true;
           }
           else
           {
               Order = false;
               previousValue = 0;
           }
        }

        if (fullSequence)
        {
            puzzleComplete = true;
        }

        //Set the now old ID as the previous Value
        previousValue = ID;

    } // end Update

    //Checks if the previous value was the previous ID in the correct order
    private bool CheckPrevious(int ID)
    {
        if((previousValue + 1) == ID)
        {
           return true;
        }
        else
        {
           return false;
        }
    }
}

Resetting Order to true all the time may be a bit over the top I guess. And there are some other details I may improve on. Like understanding JS :P

I just hope I can convey the general design one could apply for a 5 sender controller mechanism

Just apply and set all sender's messages and extract those on event (collision in this case) to the controller

I really hope this helps, it's a nice question. Love these puzzle things!

May be this code I made, didn't test it so it may not work 100%, but could give you an idea

put this on your player character ("Combine.js"):

/*
the right order.
for example
combination[0] = 1;
combination[1] = 3;
combination[2] = 2;
ect...
you set this on Hierarchy
*/
var combination : int[];

//this will change oncollision with GameObject swtich
//and will serve to compare it with the combination variable
var checkCombination : int[];

//to keep track of checkCombination
var currentOrder : int = 0;

//make sure to set checkCombination[0]... but is not really needed
function Start()
{
   checkCombination[0] = checkCombination[currentOrder];
}

function Update()
{
   //compare combinations length
   if(checkCombination.length == combination.length)
   {
      if(checkCombination == combination)
      {
         /*
         the order is right, do something...
         */
      }
      else
      {
         //if order wasn't right reset
         checkCombination[] = 0;
         currentOrder = 0;
      }
   }
}

put this to each switch ("Switch_getOrder.js"):

var getScript : GameObject; //drag your player here

//use numbers from combination[], so with this you fill checkCombination
var order : int;

function OnCollisionEnter(collision : Collision)
{
   if(collision.getScript)
   {
      var curOr = getScript.GetComponent("Combine").currentOrder;
      getScript.GetComponent("Combine").checkCombination[curOr] = order;
      getScript.GetComponent("Combine").currentOrder++;
   }
}

So... it's not 1 script controlling the switches, but it's simple and customizable via Hierarchy, and still it's only one script to be dragged to switches...

You can addapt it to use only 1 script without dragging to switches though. But i guess it would be much more "large" in coding...

Hope it helped