Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sketchy · Jul 31, 2014 at 07:20 PM · c#guibeginnermethod

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!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Jul 31, 2014 at 07:40 PM 1
Share

This is certainly a good example of the wrong way to do it.

avatar image Kiwasi · Jul 31, 2014 at 07:45 PM 0
Share

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.

avatar image Sketchy · Jul 31, 2014 at 10:10 PM 0
Share

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.

avatar image Kiwasi · Jul 31, 2014 at 10:24 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sketchy · Aug 01, 2014 at 09:59 PM 0
Share

Thank you, smallbit!!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity editor works but the built game doesnt. 0 Answers

HorizontalScrollbar not appearing in GUILayout.BeginScrollview C# 3 Answers

Get user's name in Unity using GUILayout.TextField() 2 Answers

How to remove a GUI element when button is pressed C# 1 Answer

Respawn Method not reusable between scenes ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges