Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 DairyBoy117 · Jun 07, 2016 at 09:04 AM · transform.positionstaticnon-static

Checkpoint system having problems (Non-static member)

Hello. I'm currently working on an enemy kill box that causes a respawn to last checkpoint when hit. Prior to setting up the script for the kill box, my checkpoint code worked fine. Now, with the kill box script referencing my function that restarts the level and reverts to last checkpoint, it seems that the line of code that grabs my checkpoint position is giving me the following error message:

An object reference is required to access non-static member `UnityEngine.Component.transform'

Providing my scripts for reference.

My kill box script:

 void OnTriggerEnter(){
     StartCoroutine(ballHealth.RestartLevel ());
 }

My character health script (Where the respawn code is located):

 public float maxFallDistance = -10; 
 static AudioSource audio; 
 static bool isRestarting = false; 
 static public AudioClip GameOverSound; 

 void Start (){
     audio = GetComponent<AudioSource> (); 
 }

 void Update () {
     if (transform.position.y <= maxFallDistance) {
         //UnityEngine.SceneManagement.SceneManager.LoadScene("ballGame"); 
         if (isRestarting == false) { 
             StartCoroutine(RestartLevel ()); 
         } 
     }
 }

 public static IEnumerator RestartLevel () { 
     isRestarting = true; 
     audio.clip = GameOverSound; 
     audio.Play(); 
     yield return new WaitForSeconds (audio.clip.length); 
     if (!checkpoints.isReached) { 
         UnityEngine.SceneManagement.SceneManager.LoadScene("ballGame");  
     }
     transform.position = checkpoints.ReachedPoint; //This line of code is the one causing the error
     isRestarting = false; 
 }

And the checkpoint script:

 public static Vector3 ReachedPoint; 
 public static bool isReached; 

 void OnTriggerEnter (Collider col){
     if (col.tag == "Player") { 
         if (transform.position.x > ReachedPoint.x) { 
             isReached = true; 
             ReachedPoint = col.transform.position; 
         }
     }
 }

Reading up on this error, I understand it has to do with static and non-static members. I'm new to the concept. I know variables and functions must be declared static in order to be accessed from other scripts (at least that's my current understanding, of which I know there is more to it). But I've spent the better part of my evening trying to find a solution and am not quite there. If anyone has encountered this problem and knows what direction to point me in, I'd be mighty appreciative.

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
1
Best Answer

Answer by giorashc · Jun 07, 2016 at 09:09 AM

Your co-routine is static, hence it cannot access instance members such as transform.

Remove the static keyword from the co-routine deceleration.

As a side note: Declaring static members because you need to access them from other scripts is not (always) a good idea as multiple instance of that script will share the same value (i.e. if an enemy hit points member is static then all enemies with that script will share the hit points value ALL the time)

Check this link for examples of sharing scripts data without static members.

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 DairyBoy117 · Jun 07, 2016 at 09:04 PM 0
Share

You pointed me in the right direction and I was able to learn how to call non-static members as a result. There's still some bugs I'm working out but none related to this question! So big thanks! :D

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

Why is my script only running when object has been selected beforehand? 1 Answer

Non-Static Member 1 Answer

Why is player unable to move after transform.position? 1 Answer

unity in a function I move an object using transform.position, than next (still in that function) I use that new position in a new function... But why my new function still use the old position? 0 Answers

Why does my cube not move? 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