This question was
closed Jul 15, 2017 at 07:09 AM by
tanoshimi for the following reason:
This is a very common task - please see the tutorials section for an example: https://unity3d.com/learn/tutorials/topics/scripting/getcomponent
Question by
LoganOBarr · Jul 15, 2017 at 04:39 AM ·
c#scripting problem
Using variables from different scripts in Void Update();
I can't find any examples of how to use variables from other scripts in anything but Void Start(), is there any way to use it in update?
Comment
Answer by Andrea_Marchetti · Jul 15, 2017 at 06:25 AM
Make the variable in the first script public.
Reference in your second script the gameObject you attached the first script to.
In your second script use this to get the variable:
gameObjectName.GetComponent<ScriptClassName>().publicVariableName
Answer by Voyder_Rozann · Jul 15, 2017 at 06:22 AM
Yes it's possible. Make a float Timer and then when it reaches 0 do your function or your code.
Example :
public class Example : MonoBehaviour
{
public float Timer = 3f;
void Update()
{
Timer -= Time.deltaTime;
if (Timer <= 0)
{
// Your function or code here
Timer = 3f;
}
}
}