call a class in another class
i am a beginner in unity and c#, as well as oop, i am since two days to study for this proble but i cannot find the solution, and das problem: there is a class which has just start and update:using UnityEngine; using System.Collections; public class White : MonoBehaviour { public float duration = 1.0F; public Light lt; void Start() { lt = GetComponent(); } void Update() { if(Input.GetKeyDown(KeyCode.W)) {
float phi = Time.time / duration * 2 * Mathf.PI;
float amplitude = Mathf.Cos(phi) * 3F + 3F;
lt.intensity = amplitude;}}}
i need to call this funtion, which is for adjusting the light in another class, with a if statment such as: public White whitelight;//there is in this script too //if the ray cast, then call the function light adjusting , i must stop here? if (Physics.Raycast (ray, out hits, 100f)) {
if (hits.collider.tag == "Light") {
//Debug.Log ("Raycast hit a light");
????????
how i can call this function, because the first script has just state and update method, i dont know, how ? or must i write the code again under if statment? Thank you very much for your response, please note i ma absolute beginner, if you want to answer for beginner please, thank you again.
Answer by Lord_Ford · Aug 23, 2015 at 12:18 AM
To call a class in another class you can
1: Make an instance of the class you wish to call
public class SOME_CLASS : MonoBehaviour {
public static SOME_CLASS instance;
public void Move()
{
do something...
}
}
Then from there you can simply call it
public class MY_CLASS : MonoBehaviour {
void Start()
{
SOME_CLASS.instance.Move();
}
}
2: The other way would be to reference the class like:
public class CLASS : MonoBehaviour {
public SOME_CLASS someclass;
void Start()
{
someclass.Move();
}
}
Cheers @jedimar! -PlayWell Studios
Answer by jedimar · Aug 23, 2015 at 04:12 PM
Hi, thank you very much for your post, i have tried both option, i have deleted Update, and rewrite it as public void Move(), as well as instance, is there something wrong that i do not have the Move() for calling in other class, i mean, it is not blue as you have, thank you again, you do not know how much you help me with your respond, i appreciate your advice. Thank you.
Hello and welcome. You can call your function whatever you want (dosent have to be $$anonymous$$ove), so long as it is within the set rules. If you can post your script and I will try to help you out. Cheers!
Hi, i want to know, if a class has just Start and Update, without any other method on it, how i can call it in other class, as i see, the call with instance, it is based on a $$anonymous$$ethod, but my class has just start and update, and i need call it in other class, my class is: using UnityEngine; using System.Collections;
public class Radio : $$anonymous$$onoBehaviour {
public float duration = 3.0F;
public AudioSource audio;
public static Radio instance;
void Start() {
audio= GetComponent<AudioSource>();
audio.Play();
float phi = Time.time / duration * 5 * $$anonymous$$athf.PI;
Volum = $$anonymous$$athf.Cos (phi) * 0.5 + 0.5;
audio.volume = 0.0f;
audio.Play();
}
void Update(){
}
public void $$anonymous$$ove() {
//if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R)) {
audio= GetComponent<AudioSource>();
audio.Play();
float phi = Time.time / duration * 5 * $$anonymous$$athf.PI;
float amplitude = $$anonymous$$athf.Cos (phi) * 0.5F + 0.5F;
//audio.volume = amplitude;
}
}
Thank you very $$anonymous$$uch,