- Home /
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'
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
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)
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.
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);