- Home /
C# GUI.Button Transform.Position
Hi everyone, I'm trying to make a script that moves a gameobject to a point determined by an int array . I keep getting this error from the console saying I can't implicitly convert float to vector3 . Any idea what is wrong with my code?
 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour {
     public int[] someIntArray;
     
     void Awake(){
     someIntArray = new int[]{1,2,3,4};
     }
     
     void OnGUI() {
         for (int i= 0; i < someIntArray.Length; i++){
         if (GUI.Button(new Rect(10, 20, 100, 20),someIntArray[i].ToString())){
         transform.position = transform.position[i];
             }
         }
     }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by clunk47 · Jun 09, 2013 at 03:38 AM
Because transform.position is a Vector3, and you're trying to set it to an int. You could add a Vector3 array, and be sure to either set the positions on Awake or Start, or set in the inspector since the array is public. Doing something like this would work:
 using UnityEngine;
 using System.Collections;
  
 public class Example : MonoBehaviour 
 {
     public Vector3[] someVector3Array;
     public int[] someIntArray;
  
     void Start()
     {
         someIntArray = new int[]
         {
             1, 2, 3, 4    
         };
         
         someVector3Array = new Vector3[]
         {
             new Vector3(0, 0, 0),
             new Vector3(0, 10, 0),
             new Vector3(0, 20, 0),
             new Vector3(0, 30, 0)
         };
     }
     
     void OnGUI() 
     {
        for (int i= 0; i < someIntArray.Length; i++)
         {
             if (GUILayout.Button(someIntArray[i].ToString()))
             {
                 transform.position = someVector3Array[i];
             }
         }
         
         GUILayout.Label("Position = "+transform.position);
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                