Search Unity

Cannot implicitly convert type '(float x, float y, float z)' to 'UnityEngine.Vector3'

Discussion in 'Scripting' started by AlexPig60, Dec 25, 2019.

  1. AlexPig60

    AlexPig60

    Joined:
    Dec 1, 2019
    Posts:
    2
    Hey, so I'm coding an audio visualizer and I want it so that every prefab is offset in the x position based on its position in the array. I'm not sure what I'm doing wrong and I'd appreciate it if someone could let me know what's going on here.

    The error message displayed is: Assets\Scripts\AudioVisualizer.cs(25,45): error CS0029: Cannot implicitly convert type '(float x, float y, float z)' to 'UnityEngine.Vector3'

    Here is the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AudioVisualizer : MonoBehaviour
    6. {
    7.  
    8. public AudioSource audiosc;
    9. public float[] samples = new float[128];
    10. public GameObject sampleCubePrefab;
    11. public GameObject[] sampleCubeArray = new GameObject[128];
    12. public float maxScale;
    13. public Vector3 pos; // 'pos' is a copy
    14.  
    15.     void Start(){
    16.  
    17.         pos = this.transform.position;
    18.  
    19.         for(int i = 0; i < 16; i++){
    20.                 GameObject instanceSampleCube = (GameObject)Instantiate(sampleCubePrefab);
    21.                 instanceSampleCube.transform.position = this.transform.position;
    22.                 instanceSampleCube.transform.parent = this.transform;
    23.                 instanceSampleCube.name = "SampleCube" + i;
    24.                 pos.x = (pos.x + i);
    25.                 instanceSampleCube.transform.position = (pos.x, instanceSampleCube.transform.parent.position.y, instanceSampleCube.transform.parent.position.z);
    26.                 sampleCubeArray[i] = instanceSampleCube;
    27.         }
    28.     }
    29.        
    30.     void Update(){
    31.         GetSpectrum();
    32.         for(int i = 0; i < 16; i++) {
    33.             if(sampleCubeArray != null){
    34.             sampleCubeArray[i].transform.localScale = new Vector3(10, (samples[i] * maxScale) + 2, 0);
    35.             }
    36.         }
    37.     }
    38.    
    39.     void GetSpectrum(){
    40.         audiosc.GetSpectrumData(samples, 0, FFTWindow.Blackman);
    41.     }
    42.  
    43. }
    44.  
    Hope to hear back soon.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,865
    it's this line:
    Code (csharp):
    1. instanceSampleCube.transform.position = (pos.x, instanceSampleCube.transform.parent.position.y, instanceSampleCube.transform.parent.position.z);
    You're attempting to assign 'position' which takes a Vector3 using just 3 values in parens (a tuple).

    Do something like:
    Code (csharp):
    1. instanceSampleCube.transform.position = new Vector3(pos.x, instanceSampleCube.transform.parent.position.y, instanceSampleCube.transform.parent.position.z);
    The error itself pretty much tells you this.

    This is saying that it can not convert a tuple of 3 floats into a Vector3.

    No conversion exists for that. You have to create the Vector3 yourself.
     
    AlexPig60 likes this.
  3. AlexPig60

    AlexPig60

    Joined:
    Dec 1, 2019
    Posts:
    2
    Ah, I see. Thanks!
     
unityunity