Search Unity

Type 'int' does not support slicing.

Discussion in 'Scripting' started by LogansKasten, Feb 6, 2016.

  1. LogansKasten

    LogansKasten

    Joined:
    Feb 6, 2016
    Posts:
    4
    Hi everyone I have been having an error that keeps poping up on my code (Type 'int' does not support slicing.) and I don't know how to fix it.
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var hazard : GameObject[];
    4. var spawnValues : Vector3;
    5. var hazardCount = 10;
    6. var spawnWait : float;
    7. var startWait : float;
    8. var waveWait : float;
    9. var waveEnd = false;
    10.  
    11. function Start () {
    12.     SpawnWaves ();
    13. }
    14.  
    15. function SpawnWaves () {
    16. var currFish = Random.Range(0, hazard.length);
    17.     yield WaitForSeconds (startWait);
    18.     while (true)
    19.     {
    20.         for ( var i : int= 0; i < hazardCount; i++)
    21.         {
    22.              var spawnPosition : Vector3= new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    23.              var spawnRotation : Quaternion= Quaternion.identity;
    24.             Instantiate (currFish[i], spawnPosition, spawnRotation);
    25.             yield WaitForSeconds (spawnWait);
    26.         }
    27.         yield WaitForSeconds (waveWait);
    28.         waveEnd = true;
    29.     }
    30. }
    31.  
    32. function Update(){
    33.     if(waveEnd){
    34.         hazardCount += 2;
    35.         waveEnd = false;
    36.     }
    37. }
    I am very new to coding and don't know a whole lot. Infact most of this code was used from one of Unity's official tutorial videos.

    Thanks for the help.
     
  2. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    currFish is an int, being used as a random index position in the hazard gameObject array (line 16).

    Therefore line 24 should be :
    Code (csharp):
    1. Instantiate (hazard[currFish], spawnPosition, spawnRotation);
    Really hard to tell what you want here, but that will spawn the same hazard from the array 10 times (hazardCount).
     
    LogansKasten likes this.
  3. LogansKasten

    LogansKasten

    Joined:
    Feb 6, 2016
    Posts:
    4
    I was trying to go for something that would spawn a random object from an array of object with some object spawning more often than others.
     
  4. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    One way to do that would be to use the 'fencepost' method as described here by Fattie.
    While looping through i, see if the list contains the value for i, if it does, then get a new random value for currFish.
     
    Last edited: Feb 6, 2016
    LogansKasten likes this.
  5. LogansKasten

    LogansKasten

    Joined:
    Feb 6, 2016
    Posts:
    4
    Sorry for all the questions but how would I go about finding if the list contains the value for I. Just began coding a few weeks ago.
     
  6. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    LogansKasten likes this.
  7. LogansKasten

    LogansKasten

    Joined:
    Feb 6, 2016
    Posts:
    4
    Thank you so much!