Search Unity

Need help with weapon pickup... - Solved

Discussion in 'Scripting' started by NoMaD, Feb 23, 2010.

  1. NoMaD

    NoMaD

    Joined:
    Feb 1, 2010
    Posts:
    41
    Hi. I'm new to unity and don't know much of javascript right now :oops: . Anyways in my learning project I'm trying to make weapon pickups :) . So I have my RocketLauncher as a child of Main Camera and a rocketlauncherpickup object which is just a mesh with a box collider (Trigger - true ) and this script attached to it :
    Code (csharp):
    1.  
    2.  
    3. function Start() {
    4. var realLauncher = GameObject.Find("RocketLauncher");
    5. realLauncher.SetActiveRecursively(false);
    6. }
    7.  
    8.  
    9. function OnTriggerEnter ( other : Collider) {
    10.     realLauncher.SetActiveRecursively(true);
    11.         Destroy(gameObject);
    12.     }
    13.  
    When I run it it gives me: Unknown identifier: 'realLauncher'.

    I dont know what to do :? Can someone help me solve this problem?
     
  2. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    I don't quite remember how to do it, but you have to make a reference to the gameobject before you deactivate it or Unity cant find it anymore.
     
  3. NoMaD

    NoMaD

    Joined:
    Feb 1, 2010
    Posts:
    41
    Does anyone know how to make that reference?
     
  4. Aiursrage2k

    Aiursrage2k

    Joined:
    Nov 1, 2009
    Posts:
    4,835
    Well its looking for a game object called "RocketLauncher". You are making a referance, but it is only a local variable, it means when the function goes out of scope the variable wont point to anything.

    Long story short try this.
    Code (csharp):
    1.  
    2. var realLauncher;
    3. function Start() {
    4. realLauncher = GameObject.Find("RocketLauncher");
    5. realLauncher.SetActiveRecursively(false);
    6. }
    7.  
    8.  
    9. function OnTriggerEnter ( other : Collider) {
    10.     realLauncher.SetActiveRecursively(true);
    11.         Destroy(gameObject);
    12.     }
    13.  
     
  5. NoMaD

    NoMaD

    Joined:
    Feb 1, 2010
    Posts:
    41
    YES! Thank you it works. :D