简体   繁体   中英

How Do I Cast a Variable With Dot Notation? Unity C#

I am referencing the (speed for an object in my game) variable from one script to another. There are 3 parts to it: the min var, the max var ( min and max are used to generate a random number for the speed), and the fallSpd var (the random number generated from min and max ). They are all floats, but when I reference the variable max and min on line 16, I get this error:

Assets\Destruction.cs(16,22): error CS0266: Cannot implicitly convert type 'double' to 'float'. An explicit conversion exists (are you missing a cast?)

So I cast min and it allows it. However, when I cast max to double , it gives this error:

Assets\Destruction.cs(16,9): error CS0131: The left-hand side of an assignment must be a variable, property or indexer

So I don't know how I'm supposed to cast it.

Here's the code I'm referencing (only important parts included):

using UnityEngine;
using System.Collections;
    
public class Losing : MonoBehaviour
{
    public GameObject[] Addictions = new GameObject[5];

    public Transform[] Transforms = new Transform[5];

    public static float fallSpd;

    public static float min = 0.1f;
    public static float max = 0.4f;

    private bool Lost = false;

    private void Start() 
    {
        for (int i = 0; i < 5; i++)
        {
            if (Lost != true)
            {
                Transforms[i].position = new Vector2(Random.Range(-4, 4), Random.Range(12, 31));
                ChangeSpd();
            }  
        }
    }

    public void ChangeSpd()
    {
        fallSpd = (float) Random.Range(min, max) * Time.deltaTime;
    }

And the code that references this script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
        
public class Destruction : MonoBehaviour
{
    public Transform[] Transforms = new Transform[5];

    public GameObject PrayingMan;

    public Losing losing;

    private void OnCollisionEnter2D(Collision2D other) 
    {
        Losing.min = Losing.fallSpd; 
        (double) Losing.max = Losing.min + 0.3; // i get all the errors on this line!
        print("min: " + Losing.min + "max: " + Losing.max + "speed: " + Losing.fallSpd);
        PrayingMan.GetComponent<Losing>().ChangeSpd();
    }
}

How to fix this bug?

You don't need to do any casting at all. Just change the ”0.3” to be ”0.3f”. The f indicates that it is a float, but if there is no f it will be considered a double.

You got it reversed, your variable is a float but when you do Losing.min + 0.3 you are creating a double, by default .net assumes double if no decimal modifier is specified.

You have two options, cast the value to float or use a modifier. To do the cast you would do:

Losing.max = (float)(Losing.min + 0.3);

But that is a waste of processing, the best is to specify the float modifier:

Losing.max = Losing.min + 0.3f;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM