Search Unity

help C# sript not working get component?

Discussion in 'Scripting' started by EarthenSky, Apr 22, 2014.

  1. EarthenSky

    EarthenSky

    Joined:
    Mar 11, 2014
    Posts:
    41
    help
     
  2. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    To access another script you need to create a reference and then access it.
    You need to put your script on a gameobject. Probably the case if you use a trigger.
    You should use a tag to access the GameObject as it's faster than find.

    In your second script add this:

    Code (csharp):
    1.  
    2. 1stScriptName variableName;
    3.  
    4. void Awake(){
    5.      
    6.      variableName = GameObject.FindGameObjectWithTag("yourfirstscriptwithGameObjectTagName").GetComponent<1stscriptname>();
    7. }
    8.  
    9. void Update () {
    10.  
    11.        if(variableName.egeg) // you don't need this  == true
    12.             InvokeRepeating("goo", 1 , 1);
    13. }
    14.  
    15.  
    I didn't try the code, so I don't if there are any typos.
    If you use two different script, you cannot access the variable if you don't point to the correct script.

    Hope this help.
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Stop posting duplicate threads please.
     
  4. EarthenSky

    EarthenSky

    Joined:
    Mar 11, 2014
    Posts:
    41
    {
    Code (csharp):
    1.     public Transform Target1;
    2.     public Rigidbody CubePrefab;
    3.     OnTrigAnim bup;
    4.  
    5.     void Awake ()
    6.     {
    7.         bup = gameObject.FindGameObjectWithTag("OnTrigAnimPlayer").GetComponent<OnTrigAnim>();
    8.     }
    9.     void Update ()
    10.     {
    11.         if(variableName.egeg)
    12.             InvokeRepeating("goo", 1 , 1);
    13.     }
    14.     void goo ()
    15.         {
    16.         Rigidbody cubefire;
    17.         cubefire = Instantiate(CubePrefab) as Rigidbody;
    18.         cubefire.AddForce(Target1.forward * 10);
    19.     }
    20. }

    still errors thought any way it can be fixed like the ontriganim?
     
  5. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    this is not
    Code (csharp):
    1.   bup = gameObject.
    Plese look at the script I posted.
    You need to use
    Code (csharp):
    1.  bup = GameObject.FindGameObjectWithTag
    And you should try to name your variable correctly, as it will bite you when you have a more complex project.
    This is good habits to have right at the start.