I am trying to Generate an object with a random x value but on the same y as a moving object.
this is the code I am using, Vector3 position = new Vector3(Random.Range(-5f, 1f)); it should generate an object every 5s with a x value between -5 and 1 and on the same y level as my object that is moving upwards, the error i get is 'Vector3' does not contain a constructor that takes 1 arguments, how do i fix this.
Answer by BenWiller1989 · Sep 05, 2020 at 03:08 PM
Yeah, the Vector3 wants 3 values, but you only assign one float. Use the following : int randomInt; randomInt = UnityEngine.Random.Range(-5,1); float thisHolderYFloat = gameObject.transform.position.y; Vector3 position = new Vector3((float)randomInt,thisHolderYFloat ,(float)randomInt);
edit : sorry, i don't know, if you like int or float. if you like float, use : float randomFloat; randomFloat= UnityEngine.Random.Range(-5f,1f); float thisHolderYFloat = gameObject.transform.position.y; Vector3 position = new Vector3(randomFloat,thisHolderYFloat ,randomFloat);
if you are "using System", then use UnityEngine.Random.Range()
else use Random.Range()
;
Your answer
