Access a script from another

0

I am in a unity project and I have two scripts, I would like to create a variable in the first called "counter" and the other add values, that is, as I show in the images, I have some functions that add one to the counter, and the other I want to keep the account in a separate public as I show I do not know if I explain myself but thanks for your time

    
asked by TheFPiriz 26.10.2017 в 21:56
source

3 answers

0

Classes come to the rescue!

Assuming you have a class with a variable steps paa registers the number of steps

class SoldierController extends MonoBehaviour
{

    public var pasos : float = 1;
}

and apart a class that changes the animation when you have already taken many steps, the way to get the steps that were calculated in the other controlled is created a variable of the type of the previous class.

class SoldierAnimations extends MonoBehaviour
{
    private var soldier : SoldierController;
    //soldier.pasos;
}

As always to create instances you will get an empty entry field, so you must drag your SoldierController.js script and everything will work great.

The code is in UnityScript, but surely you should not have problems passing it to C #

    
answered by 26.10.2017 в 22:18
0

It would be two classes that inherit from MonoBehaviour
Example.
Script that detects

using UnityEngine;

public class Detect : MonoBehaviour
{
  Counter counter; //Se crea una variable con la instancia de otra clase, o otro script

    void Start()
    {
        counter = GameObject.Find("ObjetoContador").GetComponent<Counter>(); //Se busca en el metodo Start o en el Awake, el objeto que lo contenga, en este caso colocamos de ejemplo "ObjetoContar", ese objeto debe tener el script.
        //Tambien se puede colocar publico y arrastrar en el inspector pero por cuestiones de buena practica lo escribo de esta manera
    }
  void OnCollisionEnter(Collision collision)
  {
    if(collision.collider.compareTag("Paso3"))
    {
      ...
      counter.count++; //aca accedo a la otra clase y sumo 1, cada que entre en colision con un objecto que contenga el tap "Paso3"
    }
  }
}

And this would be the other script

using UnityEngine;

public class Counter : MonoBehaviour
{
 public int count; //Variable entera de la clase Counter, la cual incrementara cada que entre en colision la otra clase con el tag "Paso3"
}
    
answered by 14.10.2018 в 08:46
0

I do not know if I've learned very well what you want to do, but I can recommend this for what I understood:

In the first script, put the counter variable in public: public int contador; (I recommend that the first letter of the variables be lowercase, I have seen that you have put Contador ).

In the second script, it grabs the reference of the first one. You can do it this way to make it easier: NombreDelScript cont; Then, in the Unity editor, drag the object that has the first script into the new hole that will appear when you put the previous line of code. Finally, referring to cont you can access the variable:

public void Contar(){

   cont.contador; //Esto debería acceder a esa variable con el valor actualizado por el primer script.

}

I have not worked with Unity for a long time, I hope it works and that it helps you. If you have any questions, do not hesitate to answer me. Greetings.

    
answered by 14.10.2018 в 12:16