Hedge
Hedge

Reputation: 16748

Assign script instead of GameObject in editor?

I'm connecting my GameObjects by assigning them in the editor.

All I need is the script assigned to a GameObject but I always have to do this:

public class GameBoard : MonoBehaviour {
    //assigned the gameobject containing the ScoreScript in the editor
    public GameObject totalScore;  
    private ScoreScript totalScoreScript;

    void Start() {
        totalScoreScript = this.GetComponent<ScoreScript>();
    }
}

Is there a better way to achieve this result?

Upvotes: 2

Views: 75

Answers (1)

Huacanacha
Huacanacha

Reputation: 1161

Just use ScoreScript as the public variable. To assign it drag the game object with the script attached as normal via the editor. This also works for builtin components like Transform.

public class GameBoard : MonoBehaviour {
    public ScoreScript totalScoreScript;
}

Upvotes: 2

Related Questions