0

I have one script that instantiates coins and another one for the coin itself. So once the coin itself is collected by the player, it should disappear and tell the first script to instantiate another. I tried many times to access booleans across separate scripts like so...

//script 1
public script2name scriptVar;

void start()
{
    scriptVar.GetComponent<script2name>();
}
void func()
{
    if (scriptVar.boole1 == true)
    { //create another coin 
    }
}

//script 2 (script2name)
public bool Boole1 = false;
void func()
{
    // picks up coin
    Boole1 = true;
}

I tried this however there was no response in the system, I never got another coin, it simply does not work.

Reza Ahmadi
  • 862
  • 2
  • 11
  • 23

1 Answers1

0

So I guess the only variable that is really transferrable from script to script in unity is the integer. So instead of basing it off of a true or false value, your script must trace an integer count from a separate script. Heres how your coin problem can be fixed...

The GameObject that holds script2 in this example will be necessary in the Find(""). We'll use 'GamNme' for our example.

  //script1
  public script2name Var;
  void start()
  {
  Var = GameObject.Find("GamNme").GetComponent<script2name>();
  }
  void func()
  {
   //picks up coin
   Var.count = 1;
  }

  //script2
  public int count = 0;
  void func()
  {
   if (count == 1)
   {
    //instantiate new coin and reset the counter
   count = 0;
    }
   }
  • 1
    Actually there´s no difference in integers, booleans or any other type when it comes to accessing them from another class. Instead you have to find the object those variables "life in", in your case the `script2Name`-component. – MakePeaceGreatAgain Mar 23 '20 at 18:03
  • 1
    Anyway you should definitly give your classes meanigful names. `Script` and `Script2Name` (also notice that classes should be named PascalCase) are horrible names, as they do not contain any information about what this class actually does. – MakePeaceGreatAgain Mar 23 '20 at 18:05
  • here https://stackoverflow.com/questions/61255963/how-to-really-access-info-from-seperate-scripts-in-unity-c-sharp – Alexhawkburr Apr 16 '20 at 17:09