Reset position for all players from a different script

I’m programming a simple game (the first one I do on my own) in which basically there are two scoring goals, and when a player scores, I want to reset the position for all players to some coordinates.

I have a script attached to the goals, which detects collision with the ball, as follows:

Goal.js

#pragma strict

function OnTriggerEnter2D (hitInfo : Collider2D) {
    if (hitInfo.name == "Ball")
    {
        var wallName = transform.name;
        GameManager.Score (wallName);
        hitInfo.gameObject.SendMessage ("ResetBall");
        hitInfo.gameObject.GetComponent(PlayerControlHS).ResetPlayer();
    }
}

The following script is attached to the players

PlayerControlHS.js

#pragma strict

var resetPosX : float;
var resetPosY : float;

//keys
var moveUp : KeyCode;
var moveLeft : KeyCode;
var moveRight : KeyCode;

var speed : int = 4;

function Update () {
    if (Input.GetKey(moveUp)) {
        rigidbody2D.velocity.y = speed;
    }
    if (Input.GetKey(moveLeft)) {
        rigidbody2D.velocity.x = -speed;
    }
    else if (Input.GetKey(moveRight)) {
        rigidbody2D.velocity.x = speed;
    }
    else {
        rigidbody2D.velocity.x = 0;
    }
}

function ResetPlayer () {
    Debug.Log("I'm being called");
}

Both the goals and the players have a RigidBody 2D and a collider of some kind, as well as the ball. I’ve gotten to get the function called as it is right now, but if I try to modify the position coordinates of a player, I enter a loop of death errors that when I fix one, I get another one.

That happens when I put this code in ResetPlayer():

rigidbody2D.position.x = resetPosX;
rigidbody2D.position.y = resetPosY;

Mostly, the errors are because I need an object of type PlayerControlHS or UnityEngine.GameComponent to access those fields. I’ve tried adding a variable of that type (and referencing it to each player) but it doesn’t work because it tells me I need an object to access that variable… I don’t know how to initialize it if I make it static, even though I think a static variable is a bad idea for this case since every player has it’s own parameters.

So basically I want to modify the position of the rigidbody2D for all four players.

How could I get it working?

Hi, as an alternative try passing the PlayerControlHS into the goals through the inspector, something like this…

  1. Expose it in goals.js like this
    public var PlayerControlHS : GameObject;
  2. Drag the gameobject (presumably the player) onto the goals.js script in the inspector

… let us know if that approach works.