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 jjplay175 · May 02, 2014 at 12:28 AM · c#errorfloatpubliccalling

public float being defined as object?

I have a time script which generates the time Example

 using UnityEngine;
 namespace TheTime
 {
     public class CurrentTime : MonoBehaviour
     {
     
     public float
         TimeH = 0,
         TimeM = 0,
         TimeS = 0;
     
     public bool ActiveTime,Rewind,FastForward;
     
     void Update (){
         
         if (ActiveTime)
         {
             float secondsPassed = Time.deltaTime;
 
             if (Rewind)
             {
                 secondsPassed *= -1250;
                 TimeS += secondsPassed;
 
                 if (TimeS <= 0) {
                     TimeM--;
                     TimeS += 60;
                 }
                 if (TimeM <= 0) {
                     TimeH--;
                     TimeM += 60;
                 }
                 if (TimeH <= 0) {
                     TimeH += 24;
                 }
             }

That's just a slice of it, the fast forward and rewind are the same.

Anyway I have another script which handles a clock I made

 using UnityEngine;
 using TheTime;
 
     class FFClock : MonoBehaviour {
 
     public Transform hours, minutes, seconds;
 
     void Update (){
             {
             hours.localRotation =
                 Quaternion.Euler(CurrentTime.TimeH,90,90);
             minutes.localRotation =
                 Quaternion.Euler(CurrentTime.TimeM,90,90);
             seconds.localRotation =
                 Quaternion.Euler(CurrentTime.TimeS,90,90);
         }
     }
 }
 

the 3 lines all give me

1."error CS1503: Argument #1' cannot convert object' expression to type float'" 2.error CS0120: An object reference is required to access non-static member TheTime.CurrentTime.TimeM'

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

2 Replies

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

Answer by jamiltron · May 02, 2014 at 12:43 AM

The error is not saying float is being defined as an object, it is saying:

 CurrentTime.TimeH

is an object expression when it is expecting a float. Which is true.

You are trying to reference current time's TimeH, TimeM and TimeS attributes from a static context, but they are not declared as static.

To elaborate, by saying:

  public float
 TimeH = 0,
 TimeM = 0,
 TimeS = 0;

You are declaring instance variables. That is - variables accessible from instances of the CurrentTime class. When you are trying to call

 CurrentTime.TimeH

You are trying to call a class variable in CurrentTime, of which none exist.

To do what you are doing - you'll need to declare your CurrentTime class as a singleton, so that you can have a static reference to the only instance of the CurrentTime object. I'm of the camp that singletons and mutable global state is almost always bad, though, so I offer that link in protest :P

You could also have an instance of CurrentTime in your scene, and provide a reference to it in the scripts you need, such as in FFClock.

So in FFClock, you would have something like:

 public CurrentTime currentTime;

and then you could referencing the various Time* members via:

 currentTime.TimeH


Comment
Add comment · Show 2 · 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 jjplay175 · May 03, 2014 at 03:25 PM 0
Share

I have a terrain called World which I put the CurrentTime on and FFClock on the clock

 using UnityEngine;
 using TheTime;
 
     class FFClock : $$anonymous$$onoBehaviour {
     
     public Transform hours, $$anonymous$$utes, seconds;
     public CurrentTime currentTime;
 
     void Update (){
         {
             hours.localRotation =
                 Quaternion.Euler(CurrentTime.TimeH,90,90);
             $$anonymous$$utes.localRotation =
                 Quaternion.Euler(0,90,90);
             seconds.localRotation =
                 Quaternion.Euler(0,90,90);
         }
     }
 }

I have tried everything for the past day but I'm getting a warning now saying that hours, $$anonymous$$utes, seconds and CurrentTime

Assets/FFClock.cs(7,28): warning CS0649: Field FFClock.currentTime' is never assigned to, and will always have its default value null'

and

NullReferenceException: Object reference not set to an instance of an object FFClock.Update () (at Assets/FFClock.cs:11)

avatar image jamiltron · May 03, 2014 at 04:09 PM 0
Share

Read what it says. Its literally telling you how to solve your problem -

you need to AT LEAST read the official Unity tutorials if this stuff is giving you problems, because this is VERY basic. Also - you haven't tried "everything", because the answer is incredibly simple - assign your CurrentTime script to a GameObject, and then provide a reference to that GameObject in FFClock.

avatar image
0

Answer by Maerig · May 02, 2014 at 12:49 AM

As the error is saying, you either need to make the TimeM member static, or use one which comes from an instance of the class. If your script is already attached to a GameObject in your scene, you can simply add it as a public variable

 public CurrentTime Script;

and then call

 hours.localRotation =
           Quaternion.Euler(Script.TimeH,90,90);
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

22 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

Related Questions

How can I get correct figure from formula of float 1 Answer

Multiple Cars not working 1 Answer

How do I take a public float from a different script and access it 2 Answers

Distribute terrain in zones 3 Answers

Float to int casting in C# script with modulo giving division by zero error 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