I have a script which produces 6 blocks side by side, from a possible array of 3 different blocks. The array consists of the items in my prefabs folder. When my code runs and the game starts, I need each of the blocks spawned to have a numberOfHits value that equals zero, but the prefabs in the folder are holding onto values added to them, and each item instantiated from the array holds the same number. I have tried using the start function of the prefabs but nothing is working, I think there is an issue with how I am instantiating, so here is the code for that:
EDIT: I suppose what I am also asking for is how to add +1 to an integer with the specific object I spawned, so that If I add one to a clone of a dirt block, it does not add 1 to every dirtblock in the scene or the prefab, just that specific block that the player is colliding with via raycast.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//the floor spawner will spawn a random set of different types of terrain
//the terrain will spawn in one of 6 equal length spots, next to each other
//if the player's y value changes, the next set of 6 terrain blocks will spawn
//in a set amount of space under the last set of 6 blocks
public class floorSpawner : MonoBehaviour {
//here we set arrays for the floors and the positions possible for them to spawn in
public GameObject[] floors;
public Transform[] spawnPoints; // An array of the spawn points this enemy can spawn from.
//these are the specific coordinates that a block can be spawned on
public int spawnPointSpots;
//handles the spawnZone spawning, when the player passes through these the next set of floors spawn
public GameObject spawnZone;
public Transform spawnZoneTrans;
// Use this for initialization
public void Start () {
spawnPointSpots = 0;
Spawn();
}
//spawns a random block in each slot in the row, runs in a loop till all spots are filled
void Spawn(){
Instantiate (spawnZone, new Vector3 (spawnZoneTrans.position.x, spawnZoneTrans.position.y - 4), spawnZoneTrans.rotation);
for (int i = 0; i < 6; i++) {
int floorsIndex = Random.Range (0, 3);
int spawnPointIndex = spawnPointSpots;
// Create an instance of the floor prefab at the randomly selected spawn point's position and rotation.
Instantiate (floors [floorsIndex], new Vector3 (spawnPoints [spawnPointIndex].position.x, spawnPoints [spawnPointIndex].position.y - 4), spawnPoints [spawnPointIndex].rotation);
spawnPointSpots++;
}
}
}
Making changes to small parts of the 3 scripts related worked for me, the scripts are here:
//Matthew Gigante 2017
//A helpful tutorial related to this code:
//https://unity3d.com/learn/tutorials/projects/2d-ufo-tutorial/controlling-player
//the current objective of this script is to move the player
//and to prevent them from falling off the sides of the screen.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerController : MonoBehaviour {
private int numberofhitsinscript;
//this public variable will contain the player GameObject
public GameObject player;
//this will be the speed of our player, which can be changed in the Unity inspector
public float speed;
//allows access to the floorSpawner script, which will help us instantiate new floor prefabs
public floorSpawner other;
public blockScript scriptForBlocks;
void Update ()
{
//this if/if else statement here will check to see if the player would go off the right or left of the screen
//if the player is about to go off the screen, instead put them back where in the x position they were at before that
if (player.transform.position.x <= -2.6f) {
player.transform.position = new Vector3 (-2.6f, transform.position.y, 0);
} else if (transform.position.x >= 2.6f) {
player.transform.position = new Vector3 (2.6f, transform.position.y, 0);
}
//These two if statements wait for input, and will move the player left or right if the arrows are pressed
//this is placeholder code, eventually this will be replaced with onMouseDown and buttons so that it may be optimized for android use
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
if(Input.GetKeyDown(KeyCode.DownArrow))
{
//middle vector2 is size (Vector2.one)
//creates a collider for the ground, detects overlap between player and whatever ground object is below it
//if there is an object directly under the player object, destroy it
Collider2D ground = Physics2D.OverlapBox (transform.position + Vector3.up * -0.6f, Vector2.one * .01f, 0f);
print (ground.gameObject);
//add " || bedRock(Clone) "
//if(ground.gameObject.name = "bedRock(Clone){
// play a dink noise and do not destroy bedrock
if(ground.gameObject.name != "spawnZone(Clone)"){
//scriptForBlocks.addScript ();
//scriptForBlocks.mineHandler ();
//Destroy (ground.gameObject);
ground.gameObject.GetComponent<blockScript>().addScript ();
ground.gameObject.GetComponent<blockScript>().mineHandler ();
}
}
}
//runs floorSpawner script's Start function
void OnTriggerEnter2D(Collider2D coll){
other.Start ();
}
}
//Matthew Gigante 2017
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//the floor spawner will spawn a random set of different types of terrain
//the terrain will spawn in one of 6 equal length spots, next to each other
//if the player's y value changes, the next set of 6 terrain blocks will spawn
//in a set amount of space under the last set of 6 blocks
public class floorSpawner : MonoBehaviour {
//here we set arrays for the floors and the positions possible for them to spawn in
public GameObject[] floors;
public Transform[] spawnPoints; // An array of the spawn points this enemy can spawn from.
//these are the specific coordinates that a block can be spawned on
public int spawnPointSpots;
//handles the spawnZone spawning, when the player passes through these the next set of floors spawn
public GameObject spawnZone;
public Transform spawnZoneTrans;
// Use this for initialization
public void Start () {
spawnPointSpots = 0;
Spawn();
}
//spawns a random block in each slot in the row, runs in a loop till all spots are filled
void Spawn(){
Instantiate (spawnZone, new Vector3 (spawnZoneTrans.position.x, spawnZoneTrans.position.y - 4), spawnZoneTrans.rotation);
for (int i = 0; i < 6; i++) {
int floorsIndex = Random.Range (0, 4);
int spawnPointIndex = spawnPointSpots;
// Create an instance of the floor prefab at the randomly selected spawn point's position and rotation.
Instantiate (this.floors [floorsIndex], new Vector3 (spawnPoints [spawnPointIndex].position.x, spawnPoints [spawnPointIndex].position.y - 4), spawnPoints [spawnPointIndex].rotation);
spawnPointSpots++;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class blockScript : MonoBehaviour {
public int numberOfHits;
public int maxHits;
public string currentBlock;
//public GameObject block;
public void start(){
numberOfHits = 0;
}
public void addScript(){
this.numberOfHits++;
}
public void mineHandler(){
Debug.Log(numberOfHits);
if(numberOfHits == maxHits){
Destroy (this.gameObject);
}
}
}