Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Issue with Vector 3 Array List (solved)

Discussion in 'Scripting' started by Lightupman, Jun 25, 2015.

  1. Lightupman

    Lightupman

    Joined:
    Dec 29, 2013
    Posts:
    1
    So, I'm making a very basic tile based generation system with what is probably an awful method to do so. I've just come from JS so this is supposed to be a pretty simple learning experience but I can't get past line 37.

    The error is:
    "Cannot implicitly convert type 'object' to 'UnityEngine.Vector3'. An explicit conversion exists (are you missing a cast?)"


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SceneGenerator : MonoBehaviour {
    5.     public GameObject tCross;
    6.     public GameObject straight;
    7.     public GameObject corner;
    8.     public ArrayList PossiblePositions = new ArrayList();
    9.     public ArrayList PossibleRotations = new ArrayList();
    10.     public string lastPlaced;
    11.     private GameObject lastPlacedObject;
    12.     private GameObject block;
    13.     private int blockSelector;
    14.     private Vector3 placeholder = new Vector3(0, 0, 0);
    15.     // Use this for initialization
    16.     void Start () {
    17.         firstGen();
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.    
    23.     }
    24.  
    25.     void firstGen()
    26.     {
    27.        block = Instantiate(tCross, new Vector3(0, 0, 0), Quaternion.Euler(new Vector3(-90,0,0))) as GameObject;
    28.        lastPlaced = "tCross";
    29.        findAvailableLocations();
    30.     }
    31.    
    32.     void addNewBlock()
    33.     {
    34.         blockSelector = Random.Range(1, 3);
    35.         if (blockSelector != 0)
    36.         {
    37.             placeholder = PossiblePositions[Random.Range(0, PossiblePositions.Count)];
    38.             Instantiate(tCross, placeholder, Quaternion.Euler(-90, 0, 0));
    39.         }
    40.  
    41.     }
    42.    
    43.     void findAvailableLocations()
    44.     {
    45.         if (lastPlaced == "tCross")
    46.         {
    47.             PossiblePositions.Add(new Vector3(block.transform.position.x + 3.5f, 0f, block.transform.position.z));
    48.             PossiblePositions.Add(new Vector3(block.transform.position.x - 3.5f, 0f, block.transform.position.z));
    49.             PossiblePositions.Add(new Vector3(block.transform.position.x, 0f, block.transform.position.z + 3.5f));
    50.             PossiblePositions.Add(new Vector3(block.transform.position.x, 0f, block.transform.position.z - 3.5f));
    51.             Invoke("addNewBlock", 3);
    52.  
    53.         }
    54.     }
    55.    
    56.     void printAvailablePositions()
    57.     {
    58.         for (int x=0; x < PossiblePositions.Count; x++)
    59.         {
    60.  
    61.             print(PossiblePositions[x]);
    62.         }
    63.     }
    64. }
    65.  
     
  2. psyydack

    psyydack

    Joined:
    Aug 30, 2013
    Posts:
    93
    You need to explicit what type of variable its when you in some kind of list ( like arraylist ).
    You can do this
    Code (CSharp):
    1. placeholder = PossiblePositions[Random.Range(0, PossiblePositions.Count)] as Vector3;
    or

    Code (CSharp):
    1. placeholder = (Vector3) PossiblePositions[Random.Range(0, PossiblePositions.Count)];
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You really, really don't want to use an array list. Consider a generic List instead.