- Home /
How do you CORRECTLY call methods from another C# file
so i've seen tons of different ways to call methods from other files, but I'm still really confused at which one is the correct one to use in my case. The way I have it set up right now doesn't work unfortunately. Could anyone help me out with as to why it doesn't?
Okay, I have two scripts right now Menu.cs & ScreenFadeInOut.cs. What I want to happen is to be able to click the GUI button created and displayed from Menu.cs and once clicked, call the Fade() method from ScreenFadeInOut.cs.
Here's ScreenFadeInOut.cs:
using UnityEngine;
using System.Collections;
public class ScreenFadeInOut : MonoBehaviour
{
public static ScreenFadeInOut Instance;
Shader s1;
Shader s2;
public float change = .01f;
// Use this for initialization
void Start ()
{
Instance = this;
gameObject.renderer.material.color = Color.black;
s1 = Shader.Find("Diffuse");
s2 = Shader.Find("Transparent/Diffuse");
if (gameObject.renderer.material.shader == s1)
{
gameObject.renderer.material.shader = s2;
}
}
// Update is called once per frame
void Update ()
{
}
public void Fade()
{
gameObject.renderer.material.color -= new Color(0,0,0,change);
}
}
And here's Menu.cs
using UnityEngine;
using System.Collections;
public class Menu : MonoBehaviour
{
//is the menu showing?
bool menu;
//how fast intro text scrolls.
public int txtSpeed = 30;
//button width.
private int bWidth = 200;
//button height.
private int bHeight = 50;
// Use this for initialization
void Start ()
{
//start with menu showing.
menu = true;
}
// Update is called once per frame
void Update ()
{
//if Escape key is pressed & released, hide the menu and show it if pressed again.
if (Input.GetKeyUp (KeyCode.Escape))
{
menu = !menu;
}
}
//draws all GUI on the screen
void OnGUI()
{
//if the menu is open
if (menu == true)
{
//create a button centered in the middle of the screen and if it's clicked, close the menu.
if (GUI.Button (new Rect(Screen.width/2 - bWidth/2, Screen.height/2 - bHeight/2, bWidth, bHeight), "Start"))
{
menu = false;
ScreenFadeInOut.Instance.Fade();
}
}
//intro text.
//GUI.Label(new Rect(Screen.width/2, 500 + (Time.time*-txtSpeed),500, 500),"Some long text");
}
}
As always any help is appreciated!
EDIT: the strangest part is I'm not getting any errors!
This is certainly a good example of the wrong way to do it.
Does you fade method actually do anything at all? Put a debug.log in it to check as its being called.
As sketchy as you structure is, it still should be calling fade. However dropping alpha by 0.01 every click is probably not noticeable.
if i call fade() inside the update() of ScreenFadeInOut.cs it works exactly the way i want except it does it automatically. Also from what I understand OnGUI() is called every frame just like Update() is so it shouldn't be every click it should be one click and then fade from 1 - 0 alpha incrementally.
Your reasoning is incorrect.
OnGUI is called a bunch of times per frame. However if(GUI.Button ... ) will only return true on the frame the button is clicked. Hence fade is only being called once. You can prove this by putting a debug.log inside the button if loop.
If you make fade a coroutine the script should function as intended.
Answer by smallbit · Aug 01, 2014 at 02:34 AM
Why would you get any errors? The Singleton pattern is a correct assuming you will have only 1 instance of screenfadeout script. In order for it to work you need to substract alpha in update loop like in the following script.
using UnityEngine;
using System.Collections;
public class ScreenFadeInOut : MonoBehaviour
{
public static ScreenFadeInOut Instance;
Shader s1;
Shader s2;
public float change = .01f;
bool isFadingOut = false; //
bool isFadingIn = false;
// Use this for initialization
void Start()
{
Instance = this;
gameObject.renderer.material.color = Color.black;
s1 = Shader.Find("Diffuse");
s2 = Shader.Find("Transparent/Diffuse");
if (gameObject.renderer.material.shader == s1)
{
gameObject.renderer.material.shader = s2;
}
}
// Update is called once per frame
void Update()
{
if (isFadingOut)
{
if (gameObject.renderer.material.color.a > 0)
gameObject.renderer.material.color -= new Color(0, 0, 0, change);
else
isFadingOut = false;
}
if (isFadingIn)
{
if (gameObject.renderer.material.color.a < 255)
gameObject.renderer.material.color -= new Color(0, 0, 0, change);
else
isFadingIn = false;
}
}
public void FadeOut()
{
isFadingOut = true;
}
public void FadeIn()
{
isFadingIn = true;
}
}
If you don't need to do anything else in methods fadeout and fade in you can just make bools as public and change them from other script.