Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Zaxs_Reaper · Apr 04, 2017 at 08:32 PM · referencenon-staticmember

An object reference is required to access non-static member

I'm trying to connect the coroutine of this script from another one. So I learned that I need to change the IEnumerator Fade to public static. Now it just spawned another error. I looked for answers and I can only find get.component solutions but I think that it will not work in Fade Here is the script. Early thank you so much for helping me. ^_^

 GameObject Player;
 public string SceneName;
 public float secBeforeFade = 3.0f;
 public float fadeTime = 5.0f;
 public Texture fadeTexture;
 private bool fadeIn = false;
 private float tempTime;
 private float time = 0.0f;

 void Start ()
 {
     Player = GameObject.Find ("Player");
 }

 public static IEnumerator Fade()
 {
     yield return new WaitForSeconds(secBeforeFade);
     fadeIn = true;
 }

 void Update()
 {
     if (fadeIn)
     {
         if (time < fadeTime) time += Time.deltaTime;
         tempTime = Mathf.InverseLerp(0.0f, fadeTime, time);
     }

     if (tempTime >= 1.0f)
         SceneManager.LoadScene(SceneName);
 }

 void OnTriggerEnter (Collider col)
 {
     
     if (col.tag == "Player")
     {
         StartCoroutine(Fade());
     }
 }

 void OnGUI()
 {
     if (fadeIn)
     {
         Color colorT = GUI.color;
         colorT.a = tempTime;
         GUI.color = colorT;
         GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), fadeTexture);
     }
 }

}

Comment
Add comment
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

1 Reply

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

Answer by PizzaPie · Apr 05, 2017 at 01:42 PM

You don't have to make the function static. All you have to do is make a reference of this instance of this class and call the function from there, something like this:

 public class someClass : MonoBehaviour
 {
     public OtherClass otherClass; // this is the reference of the class you provide, replace the OtherClass with the class's name
 
     void someFunction()
     {
         StartCoroutine(otherClass.Fade());  //here you call the IEnumarator
     }
 }

You need to fill the reference either you do it through the editor or through code.

Note1: make sure to remove the static keyword from your script.

Note2: the reason it doesn't work when it is static is that it modifies an non static field (the bool fadeIn) which produces the error because the compiler does not know in which instance of the class should modify that field.

Cheers hope that helps.

Comment
Add comment · 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

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

100 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

object reference is required to access non-static member - What does this mean? 1 Answer

RROR - An object reference is required to access non-static member, how do i fix? 0 Answers

non-static member error 1 Answer

How to pass reference to variable in IEnumerator method in C#? 2 Answers

Compute Shader Pass RWStructuredBuffer by Reference 1 Answer


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