Setting GameObject as target in another script


I have two scripts, PlayerController.cs and Target.cs in PlayerController there is a public GameObject target;

In Target.cs I want to set the gameobject I click as target in the PlayerController script, but obviously I messed up somewhere.. anyone have a hint?

The first line works just fine as far as I can see. Debug.Log prints out the name of the object I click. I am also able to access target and other int's etc from the PlayerController using pc. so I guess the third line works? However I cant set the target of PlayerController to "go"

Error: NullReferenceException: Object reference not set to an instance of an object


    void OnMouseDown() {
    GameObject go = this.gameObject;
    PlayerController pc = (PlayerController)GetComponent("PlayerController"); = go;

Anyway, thanks for looking

Ah yes, you were on the right track though. Just missing the step of getting a reference to your player, to get it's script component.

void OnMouseDown()
   GameObject.Find("ThePlayerObjectsName").GetComponent<PlayerController>().target = gameObject;

   // Or if you have the Player setup with a Tag

   GameObject.FindWithTag("TagName").GetComponent<PlayerController>().target = gameObject;


coulfd you place the whole scripts please ? that wouyd be greatfull