Search Unity

Assign the player GameObject in the Inspector by script

Discussion in 'Scripting' started by Danim3D, Aug 19, 2011.

  1. Danim3D

    Danim3D

    Joined:
    Sep 7, 2009
    Posts:
    18
    I'm trying to find the syntax to assign the player GameObject in the Inspector player variable?
    Thank if anyone can help!

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MyScript: MonoBehaviour{
    5.     public GameObject player;
    6.  
    7.     void OnTriggerEnter(Collider hit){
    8.         [COLOR="red"]player.gameObject = hit.gameObject;[/COLOR]
    9.     }
    10. }
     
  2. pezz

    pezz

    Joined:
    Apr 29, 2011
    Posts:
    606
    I am not understanding your question.

    Can you explain some more :)
     
  3. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    if its a public variable it will show up in the inspector. You drag the gameobject onto it. you dont need to put player.gameObject either, because it already is a gameObject
     
  4. Danim3D

    Danim3D

    Joined:
    Sep 7, 2009
    Posts:
    18
    I understand I can drag the gameobject onto a public variable but I want to do it during gameplay using script.
    Because I want this public GameObject variable to change during gameplay.
     
  5. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    oh well when you want to change it just put player = someObject; and it will change it to that object
     
  6. Danim3D

    Danim3D

    Joined:
    Sep 7, 2009
    Posts:
    18
    Ok thank, I was not able to do it because "hit" is a Collider.
    Like this it work!
    player = hit.gameObject;