Modifying A vector3 variable C#

Hello! I’m trying to modify a Vector3 variable so it spawns an object at random coordinates, however it gives me errors about casts floats, and doubles. Does anyone know how to correctly modify a Vector3 variable? Sorry for all the question asking.

using UnityEngine;
using System.Collections;

public class BacteriaParts : MonoBehaviour {
    public GameObject ribosome;
    public GameObject ribo1;
    public Vector3 spawn1;
    public string ribo1ID;
    // Use this for initialization
    void Start () {
        ribosome = GameObject.Find ("Ribosome");
        if (ribosome == null) {
            Debug.Log ("A Ribosome to copy can't be found");
        }
        else {
            Debug.Log
                ("The Instance Id of the Ribosome is" + ribosome.GetInstanceID().ToString());
        }
        //Set spawn coordinates of Ribosome
        spawn1.x +=0.01 * Random.Range (1, 10);
            spawn1.y +=0.01 * Random.Range(1, 10);
                spawn1.z += -1;

        //Instantiate the Ribosome
            ribo1 = Instantiate (ribosome, spawn1, Quaternion.identity);
        //Save ID for other scripts to use
        ribo1ID = ribo1.GetInstanceID().ToString();
        Debug.Log ("ribo1 has an InstanceID of" + ribo1ID);
    }
   
    // Update is called once per frame
    void Update () {
   
    }
}

Vector3 is composed of 3 floats
0.01 is a double
0.01f is a float

1 Like

Change 0.01 to 0.01f. Decimal values default to double so you have to tell it to be a float
Add “as GameObject” to the end of your Instantiate call. Instantiate returns an Object that you need to cast to whatever you passed in.

1 Like

Thank you guys so much! It works now!