Cannot convert 'object' expression to 'float'
error CS0236: A field initializer cannot reference the nonstatic field, method, or property randomGen.blockPos' error CS1502: The best overloaded method match for
UnityEngine.Vector3.Vector3(float, float)' has some invalid arguments error CS1503: Argument #2' cannot convert
object' expression to type `float'
The code: float blockPos = 6f; Vector3 placeBlockPos = new Vector3(0, blockPos);
Answer by RudyTheDev · Sep 22, 2015 at 09:03 PM
You can't use other variables for field values.
public class X : MonoBehaviour
{
private float blockPos = 6f;
//private Vector3 placeBlockPos = new Vector3(0, blockPos); -- NO
private const float blockPosConst = 6f; // can't modify anywhere
private Vector3 placeBlockPosConst = new Vector3(0, blockPosConst); // okay
}
Basically, the value of blockPos
is not yet known for sure when the value of placeBlockPos
is being assigned and vice versa. Otherwise you could do stuff like:
public class X : MonoBehaviour
{
private float a = b;
private float b = a;
}
Which is clearly invalid.
If you need to do this, do it in Start()
or Awake()
Your answer
Follow this Question
Related Questions
ERROR CS0029: Cannot implicitly convert type `UnityEngine.GameObject' to `TriggerSphere' 1 Answer
NullReferenceException: Object reference not set to an instance of an object 3 Answers
How to make a number higher than Quintillion? (1000000000000000000) 1 Answer
Object Reference Error 1 Answer
[Begginer] [Solved] How to solve a Null Reference Exception 1 Answer