Search Unity

Accessing component

Discussion in 'Scripting' started by AaronC, Jul 27, 2007.

  1. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Hi
    Im a bit stuck here. Trying to turn on and off a component via trigger:

    Code (csharp):
    1.  
    2. var otherscript:GameObject;
    3. var otherscript1:GameObject;
    4. function OnTriggerEnter () {
    5.     otherScript = GetComponent(SmoothLookAt);
    6.     otherScript.active=true;
    7.      otherScript1 = GetComponent(SmoothLookAt);
    8.     otherScript1.active=true;
    9. }
    10. function OnTriggerExit () {
    11.     otherScript = GetComponent(SmoothLookAt);
    12.     otherScript.active=false;
    13.       otherScript1 = GetComponent(SmoothLookAt);
    14.     otherScript1.active=false;
    15. }
    Its the null ref exception:Object ref not set to an instance...Is it because Im accessing the same (SmoothLookAt )Script on two GO's...?

    Thank you
    AC
     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    It's generally a bad idea to have more than one instance of a script attached to the same GO. Are you using the trigger to switch back and forth between different settings? In this case, it might be a good idea to build that functionality into the SmoothLookAt script.
     
  3. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    They are not in the same object, its one script attached to two different objects.

    Code (csharp):
    1. var otherscript:GameObject;
    2. var otherscript1:GameObject;
    3. function OnTriggerEnter () {
    4.     otherScript = GetComponent(SmoothLookAt);
    5.     otherScript.active=true;
    6.      otherScript1 = GetComponent(SmoothLookAt);
    7.     otherScript1.active=true;
    8. }
    9. function OnTriggerExit () {
    10.     otherScript = GetComponent(SmoothLookAt);
    11.     otherScript.active=false;
    12.       otherScript1 = GetComponent(SmoothLookAt);
    13.     otherScript1.active=false;
    14. }
    GetComponent get the component of some object, if you use GetComponent alone, it will search in the object this script is attached to.

    On the other hand, GetComponent returns a Component, base clase for all components (including SmoothLookAt) and your variables are declared as GameObject.

    You should have something like this to (de)activate components

    Code (csharp):
    1. var object : GameObject; // setted in the inspector to be the object in which lays the component to be (de)activated
    2.  
    3. private var script : ComponentName;
    4.  
    5. function Awake ()
    6. {
    7.    script = object.GetComponent (ComponentName); // cache the component
    8. }
    9.  
    10. function OnTriggerEnter ()
    11. {
    12.    script.enabled = true;
    13. }
    14.  
    15. function OnTriggerExit ()
    16. {
    17.    script.enabled = false;
    18. }
    .ORG
     
  4. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552