All crops in list growing at once when they are supposed to grow individually
Hey there, I am having some trouble separating crops from each other with lists, once one reaches the end of its time needed to grow (10 presses of 'G' for testing) all crops disappear instead of just one, even though they are placed after I press 'G' a couple of times. I also noticed that if I have 2 seeds placed, every time I press 'G' the counter goes down by 2 instead of 1. The same idea for the more seeds placed at once. It has been over a year since I have looked at arrays and lists so if the answer is obvious I apologize but I have scoured the internet for hours looking for solutions related to tiles and tilemaps to no resolution. Any help really appreciated. Many thanks, Marshall. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Tilemaps; using System.Collections; using System.Collections.Generic;
public class PlayerPlanting : MonoBehaviour
{
PlayerMovement playerMovement;
public Tile hoedTile;
public Tile cropTile;
public Tilemap tileMap_Base;
public Tilemap tileMap_Hoed;
public Tilemap tileMap_Crops;
public Vector3Int cellPosition;
public Vector3Int currentCell;
public Vector3Int cropPosition;
public int growthLeft = 10;
public List<Vector3Int> plantPositions = new List<Vector3Int>();
void Update()
{
foreach(Vector3Int pos in plantPositions)
{
cropPosition = pos;
if (Input.GetKeyDown(KeyCode.G))
{
growthLeft = growthLeft - 1;
Debug.Log("growth left:" + growthLeft);
}
if(growthLeft <= 0)
{
Debug.Log("fully grown");
tileMap_Crops.SetTile(cropPosition, hoedTile);
plantPositions.Remove(cropPosition);
}
}
}
void LateUpdate()
{
HoeGround();
}
public void HoeGround()
{
// get current grid location
currentCell = tileMap_Base.WorldToCell(transform.position - new Vector3(0, 3, 0));
//NOT APPLICABLE
//if (playerMovement.horizontalMovementD == -1 || playerMovement.horizontalMovementD == 1 || playerMovement.verticalMovementD == -1 || playerMovement.verticalMovementD == 1)
//{
// currentCell.x = currentCell.x + playerMovement.horizontalMovementD;
// currentCell.y = currentCell.y + playerMovement.verticalMovementD;
//}
if (Input.GetKeyDown(KeyCode.Space) && !tileMap_Hoed.HasTile(currentCell))
{
// set the new tile
tileMap_Hoed.SetTile(currentCell, hoedTile);
}
else if (Input.GetKeyDown(KeyCode.Space) && tileMap_Hoed.HasTile(currentCell) && !tileMap_Crops.HasTile(currentCell))
{
tileMap_Crops.SetTile(currentCell, cropTile);
plantPositions.Add(currentCell);
}
}
}
Your answer
Follow this Question
Related Questions
Trying to program two buttons to appear when the player in my game dies 0 Answers
Only spawning power ups that the player wants in that game 1 Answer
C# List index confusion using Mathf.Clamp 1 Answer
Why is my List adding to same array object again and again in update? 1 Answer
Move Object around a platform of tiles 0 Answers