Disabling Script on many Objects one at a time.

In my project i have many platforms and they all share same script.

What i’m trying to achieve is that when player collides with the platform the script that is attached to the platform will deactivate. But only that one script on that specific platform the player is standing on. All the other scripts, on other platforms he is not standing on, should be activated until player collides with them.

Thank you guys for any answers or ideas on how to solve this problem.

So far i have:

public GameObject[] cubes;       
public Script theScript;

Start () 
{
    cubes = GameObject.FindGameObjectsWithTag("cubes");
    theScript = ?
}

void OnCollisionEnter2D(Collision2D other) 
{
    if(other.gameObject.tag == "cubes")
    {
    theScript.enabled = false
    }
}

I’m assuming the script you gave is attached to the Character/Player.

  1. You don’t have to do anything on Start()
  2. Change the type of “theScript” variable to the name of the script you want to get.

Change your collision function to this:

void OnCollisionEnter2D(Collision2D other) 
{
    if(other.gameObject.tag == "cubes")
    {
        //Change Script for the name of the script of the platform
        theScript = other.gameObject.GetComponent<Script>(); 
        theScript.enabled = false;
    }
}