Working on a 3D audio visualizer project (Unity) and I can’t convert Vec3 to Float, what am I doing wrong?

Noob here… I am working with a c# script in Unity which was originally written to scale an object on 2 separate axes (X/Z) using 2 separate variables (based on audio input). The change I am trying to make is to have that object scale in all axes uniformly.

I tried changing the variables from Floats to Vec3 and removed the trailing x and z in the localScales on the last line to make it uniform scaling. But as the title mentions, Unity says it can’t convert a Vec3 to a float. I think I understand the problem, but not the solution. I’ve looked at other similar posts and experimented but have come up empty.

I foresee changing these 2 variables to be uniform XYZ scales will multiply the effect — I think I’m okay with that, although I may want to try to make it use a single variable to do the scaling eventually.

Thank you to anyone willing to help me, I’ve been learning Python but haven’t gotten much into c# yet.

This is what the code looks like now (includes my changes):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static SAP.SapVars;


    public class GrowSap : MonoBehaviour
    {
        private RedactedForPrivacy sap;
        public SapSetBand growBand;
        public int band;
        public Vector3 height;
        public Vector3 baseHeight;
        private float _spectrumBand;

        // Start is called before the first frame update
        void Start()
        {
            sap = RedactedForPrivacy._SapInstance;
        }

        // Update is called once per frame
        void Update()
        {
            switch(growBand.bandType)
            {
                case BandType.Raw:
                    _spectrumBand = sap.GetSpectrumBand(growBand.source, growBand.spectrum, band);
                    break;

                case BandType.Smooth:
                    _spectrumBand = sap.GetSpectrumBandSmooth(growBand.source, growBand.spectrum, band);
                    break;

                case BandType.Trigger:
                    _spectrumBand = sap.GetSpectrumBandTrigger(growBand.source, growBand.spectrum, growBand.trigger, band);
                    break;
            }

        this.transform.localScale = new Vector3(this.transform.localScale, baseHeight + _spectrumBand * height, this.transform.localScale);
    }
    }

This is what the original code looked like (height and baseHeight were formerly floats, and the local scale transformations on the last line stipulated .x and .z axes)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static SAP.SapVars;


    public class GrowSap : MonoBehaviour
    {
        private RedactedForPrivacy sap;
        public SapSetBand growBand;
        public int band;
        public float height;
        public float baseHeight;
        private float _spectrumBand;

        // Start is called before the first frame update
        void Start()
        {
            sap = RedactedForPrivacy._SapInstance;
        }

        // Update is called once per frame
        void Update()
        {
            switch(growBand.bandType)
            {
                case BandType.Raw:
                    _spectrumBand = sap.GetSpectrumBand(growBand.source, growBand.spectrum, band);
                    break;

                case BandType.Smooth:
                    _spectrumBand = sap.GetSpectrumBandSmooth(growBand.source, growBand.spectrum, band);
                    break;

                case BandType.Trigger:
                    _spectrumBand = sap.GetSpectrumBandTrigger(growBand.source, growBand.spectrum, growBand.trigger, band);
                    break;
            }

        this.transform.localScale = new Vector3(this.transform.localScale.x, baseHeight + _spectrumBand * height, this.transform.localScale.z);
    }
    }

Thank you to anyone willing to help me

New contributor

Jeremy Romanowski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

The main issue is that Vector3 and float are completely different types, a float is a single decimal value, while Vec3 is a set of 3 ordered vectors/decimals, representing 3 axis: x, y, and z (e.g. <0.1, 0.1, 0.1>).

In the following line:

this.transform.localScale = new Vector3(this.transform.localScale, baseHeight + _spectrumBand * height, this.transform.localScale);

There are 2 main issues:
First, you’re trying to multiply height and baseHeight which are sets of 3 values, by a float: _spectrumBand to be assigned as an axis (a float) of a new Vector3.

Secondly, by removing the axis specifiers (.x, and .z) from this.transform.localScale, means that you’re now using the whole vector as the value for the single axis.

So the solution is to convert this vector to a float by some equation, like through the magnitude of the vector for example.

Vector3 newVec = baseHeight + _spectrumBand * height;
this.transform.localScale = new Vector3(this.transform.localScale.x, newVec.magnitude, this.transform.localScale.z);

Or any function of the different axis of these vectors as single values for each axis in the resulting this.transform.localScale vector.

Hope this helps!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật