Error: Cannot implicitly convert type 'UnityEngine.Transform' to int.

I am trying to convert private int to transform but get following error: Cannot implicitly convert type ‘UnityEngine.Transform’ to int. Anyone help?

Here is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SofaModelsView : MonoBehaviour
{
    public Transform[] views;
    public float transitionSpeed;
    Transform currentView;
    private int CurrentSofaModelViews;
    public GameObject[] SofaModelName;
    // Use this for initialization
    void Start()
    {
        for (int i = 0; i < views.Length; i++)
        {
            views[I].gameObject.SetActive(false);
            CurrentSofaModelViews = 0;
            views[0].gameObject.SetActive(true);
        }
        currentView = views[0];
        SofaModelName[0].SetActive(true);
    }
    void LateUpdate()
    {
        //Lerp position
        transform.position = Vector3.Lerp(transform.position, currentView.position, Time.deltaTime * transitionSpeed);
        Vector3 currentAngle = new Vector3(
         Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentView.transform.rotation.eulerAngles.x, Time.deltaTime * transitionSpeed),
         Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentView.transform.rotation.eulerAngles.y, Time.deltaTime * transitionSpeed),
         Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentView.transform.rotation.eulerAngles.z, Time.deltaTime * transitionSpeed));
        transform.eulerAngles = currentAngle;
    }
   
    public void NextButton()
    {
        views[CurrentSofaModelViews].gameObject.SetActive(false);
        if (CurrentSofaModelViews >= views.Length - 1)
        {
            CurrentSofaModelViews = 0;
            views[CurrentSofaModelViews].gameObject.SetActive(true);
            Debug.Log ("You has switched to next sofa model");
        }
        else
        {     
            CurrentSofaModelViews++;
            views[CurrentSofaModelViews].gameObject.SetActive(true);
            //Debug.Log ("Function Part 2  array lenght = " + characterModels.Length.ToString());
            CurrentSofaModelViews = currentView;   -------- Here is the error.
        }
    }   
}

[code/][/I]

Use code tags:

Also the exception tells you exactly what the problem is, and should also tell you what line of code it’s at.

You even say you’re doing it… that you’re converting an ‘int’ to a ‘transform’.

Ummm… what do you expect to happen? Transform’s and int’s are nothing alike.

Hi @lordofduct , sorry I’m beginner in writing c# script.
I was trying to do a next and prev buttons to preview all five types of sofa that I created, I was using transform array to define each type of sofa view and link them to my main camera. However, it can only load one transform for one UI button, what I want to do is using only prev and next buttons to preview all the sofa accordingly. That is the reason I used private interger(what I found on internet). But I have no idea how to convert int to transform.

You can’t convert int to transform…

Are you trying to set currentView = views[CurrentSofaModelViews]?

Your int should point to the active element of the array\list, (index)

When the number change just deactivate the currently active and make sofa numberindex appear

yup, i know that is a error but have no idea to figure it out.

What do you mean? Is what I posted what you are trying to do? if so, that is the code…Set currentview to the array value using the int as your index.

Can you write it in programming language?

I just did…

currentView = views[CurrentSofaModelViews];

Thanks @Brathnann , now the button only worked after fifth times I pressed on it, and it jump to the fifth object that I created, you know what is the problem?

Thanks guys I have fix it and it work perfectly. Thanks for all your help! :):slight_smile:

Glad you got it working :slight_smile: