Search Unity

help how do I get a bool to access 2 scripts

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

  1. EarthenSky

    EarthenSky

    Joined:
    Mar 11, 2014
    Posts:
    41
    help I have a public bool but it doesn't access the other script

    here's the script

    Code (csharp):
    1. {
    2.     public bool egeg = false;
    3.     void OnTriggerStay ()
    4.     {
    5.         if(egeg == false)
    6.             egeg = true;
    7.     }
    8. }


    and the other one


    Code (csharp):
    1.     public Transform Target1;
    2.     public Rigidbody CubePrefab;
    3.     void Update ()
    4.     {
    5.             if(egeg == true)
    6.             InvokeRepeating("goo", 1 , 1);
    7.     }
    8.         void goo ()
    9.         {
    10.         Rigidbody cubefire;
    11.         cubefire = Instantiate(CubePrefab) as Rigidbody;
    12.         cubefire.AddForce(Target1.forward * 10);
    13.     }
    14. }
     
  2. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    on your 2nd script you have to referenc your boolean first depending on the gameObject where the script is, it will be something like this:

    2nd script:
    Code (csharp):
    1.  public Transform Target1;
    2.  
    3.     public Rigidbody CubePrefab;
    4.  
    5.     void Update ()
    6.     {
    7.             if( GameObject.Find("MyGameObject").GetComponent(FirstScript).egeg == true)
    8.             {
    9.                      InvokeRepeating("goo", 1 , 1);
    10.             }
    11.     }
    12.  
    13.         void goo ()
    14.         {
    15.              Rigidbody cubefire;
    16.             cubefire = Instantiate(CubePrefab) as Rigidbody;
    17.             cubefire.AddForce(Target1.forward * 10);
    18.  
    19.         }
    So this should be working but of course, if you plan to use it in an update, try to reference your script first for better performances
     
  3. EarthenSky

    EarthenSky

    Joined:
    Mar 11, 2014
    Posts:
    41
    for me it didn't work and It came up with errors?

    any thing else
     
  4. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    I answered in another thread. Please make sure you don't create duplicate ...