Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Assigning script to player

Discussion in 'Scripting' started by kwabbott, Dec 16, 2007.

  1. kwabbott

    kwabbott

    Joined:
    Jun 5, 2007
    Posts:
    151
    I'd like this script to identify when the player enters the trigger, then assign a script to the player.

    First I wrote this and it works great
    Code (csharp):
    1.  
    2. var receiver : GameObject;
    3.  
    4. function OnTriggerEnter (other : Collider) {
    5.     if (other.CompareTag ("Player")){
    6.     receiver.AddComponent("ScalePlayer");
    7.     }
    8.     Destroy(gameObject);
    9. }
    10.  
    However, I'd like to remove the revealed variable so that any object with the Player Tag will get the new script. As usual I am messing up the syntax. It seems this should work but it give an error "AddComponent is not a member of Boolean"

    Code (csharp):
    1.  
    2. function OnTriggerEnter (other : Collider) {
    3.     var receiver =  (other.CompareTag ("Player"));
    4.     if (other.CompareTag ("Player")){
    5.     receiver.AddComponent("ScalePlayer");
    6.     }
    7.     Destroy(gameObject);
    8. }
    Can anyone help?

    thanks

    Kevin
     
  2. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    It's because CompareTag returns a boolean (true, false) and you are assigning that to the receiver var.

    This is what you want:

    Code (csharp):
    1.  
    2. function OnTriggerEnter (other : Collider) {
    3.     if (other.CompareTag ("Player")){
    4.     other.gameObject.AddComponent("ScalePlayer");
    5.     }
    6.     Destroy(gameObject);
    7. }
    -Jeremy
     
  3. kwabbott

    kwabbott

    Joined:
    Jun 5, 2007
    Posts:
    151
    I could swear that I tried that at one point (takes deep breath).

    Thanks Jeremy,


    Kevin