Repeated script doesn't work in the scene

Hello everyone,

I was trying to make a ladder script for an 2D platform game, the script initially works properly when I applied to an gameObject (a ladder sprite) but once I duplicate the gameObject with the script doesn’t affect to the character,but if I delete the original gameObject then it works.

the script is the following:

using UnityEngine;
using System.Collections;

public class ladder : MonoBehaviour {

	public movementScript movementScript;
	public GameObject Player;
	private bool grav = false;

	// Use this for initialization
	void Awake () 
	{
		movementScript = Player.GetComponent<movementScript> ();
	}
	
	// Update is called once per frame
	void FixedUpdate () 
	{
		if (grav == true)
		{
			movementScript.ladder = true;
		}else
		{
			movementScript.ladder = false;
		}
	}
	void OnTriggerEnter2D(Collider2D other)
	{
		grav = true;
	}
	void OnTriggerExit2D(Collider2D other)
	{
		grav = false;
		movementScript.jump = 1;
		Player.rigidbody2D.gravityScale = 1.0f;
	}
}

and the call from the character script is the following:

	void Ladder()
	{
	if (ladder == true)
	{
		movement = false;
		if (Input.GetKey (KeyCode.W)) 
		{
			transform.Translate(Vector2.up * 10f * Time.deltaTime);
			this.rigidbody2D.gravityScale = 0.0f;
		}
		if (Input.GetKey (KeyCode.S)) 
		{
			transform.Translate(-Vector2.up * 10f * Time.deltaTime);
			this.rigidbody2D.gravityScale = 0.0f;
		}
	}else
		{
			movement = true;
		}
	}

both gameObjects have assigned the player and its script in the inspector. Why is that caused?

you could simplify this a bit, might make it easier to debug. You will need to tag your player gameobject as “Player” for this to work. If your player gameobject is already tagged as something, just change the code accordingly

	void OnTriggerEnter2D(Collider2D other)
	{
		if(other.gameObject.tag == "Player"){
			other.gameObject.GetComponent<movementScript>().ladder = true;
		}
	}
	void OnTriggerExit2D(Collider2D other)
	{
		if(other.gameObject.tag == "Player"){
			other.gameObject.GetComponent<movementScript>().ladder = false;
		}
	}

Keeping the ladder as a separate object from the player (and its controls) will make debugging things much easier. The player controls “player” things, the ladder acts as a trigger for the player.

That way, you could disable climbing any ladder all together by not calling “Ladder()” in the player script.