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 Nzingha · Jul 27, 2014 at 07:34 PM · timerseconds

Referencing other scripts: -5 seconds rule on timer

Hello Unity Users, I need help with my game timer. In my game, when the ball hits the finishline trigger, what is suppose to happen is:

  • The ball is destroyed

  • minus 5 seconds from the Count-Up timer

I dont know why its giving me problems. I am referring The timer from my working timer script into my finishline script. I keep getting this error.

Assets/Scripts/Finishline.cs(30,32): error CS0120: An object reference is required to access non-static member `Clock2.OnGUI()'

Please Help! Novice Unity User

 public class Finishline : MonoBehaviour 
 {
 //declare scoring variables
     public int count;
     public GUIText countText;
     //Script Reference
     public int Clock2.OnGUI.Dseconds;
     public Clock2 Timer;
 
     void Start () 
     {
         count = 0;
         SetCountText();
     }
 
 //if the balls collides with a finishline collider, finishline destroys the balls and add a point to score.
         
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == "Ball") 
         {
             Debug.Log ("Object Entered the trigger");
             count += 1;
 
             // subtract 5 seconds whenever a ball enters the finishline
             Clock2.OnGUI().Dseconds = -5;
             SetCountText ();
             //gameObject.SetActive(false);
             Destroy (other.gameObject);
         }
         if (count > 12) 
         {
                 Time.timeScale = 0;
                 
         }
     }
 
     void SetCountText()
     {
         countText.text = "Number of Balls Collected: "+ count.ToString();
         //if(count>11)
         //    {
         //    winText.text = "You Win!";
         //    }
     }
     
 }    



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 fafase · Jul 27, 2014 at 07:38 PM

  Clock2.OnGUI().Dseconds = -5;

Here something is wrong. You cannot (or at least should not) use a method with a name already used by Unity.

I don't quite get what you are after since you want to call the method and then a sub variable which cannot be done in this case.

OnGUI is already taken care by Unity engine so you are not supposed to call yourself, you might as well create a new method with new naming.

Finally I think you are after a common case which is accessing variables from other scripts. You could read the link below I think you will find what you are after.

http://unitygems.com/script-interaction1/

Comment
Add comment · Show 3 · 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 Nzingha · Jul 27, 2014 at 11:58 PM 0
Share

The Dseconds that I am trying to change is within the OnGUI() in my Clock2 script. I looked at the website and I am not sure how else I can get to this local variable Dseconds. How could I rename OnGUI?

avatar image fafase · Jul 28, 2014 at 08:35 AM 0
Share

The variable is in the script, I would think it is not declared inside the method as it would not make sense.

Then you can use the GetComponent method.

avatar image Nzingha · Jul 28, 2014 at 01:07 PM 0
Share

Thanks fafase for helping me out. You are right that I need to use GetComponent method but I am trying to reference a variable. From Statement response from another page, I need to refer to the variable through a gameObject then use the GetComponent method.

http://answers.unity3d.com/questions/42843/referencing-non-static-variables-from-another-scri.html

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

2 People are following this question.

avatar image avatar image

Related Questions

Turn seconds into minutes and seconds MM:SS 1 Answer

I'm trying to get and store max elapsed time in min:sec format. Also, the best time for Highscore. Not working! 2 Answers

Timer's tenths of seconds visible digits issue 3 Answers

Call function only if last call was >= X seconds ago 1 Answer

How to stop a Countdown Timer? 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