I’m pretty new to unity and I don’t really know c# very well (I’ve mostly just been figuring stuff out from my limited knowledge of javascript). I’m making my own version of Brick Breaker and I’m trying to make a type of brick (that resembles a mask) where, after being hit once, it’ll teleport to 1 of 8 possible locations in an array. I’m not sure if I’ve been making and/or using the array wrong or if I shouldn’t even be using an array for this. I would appreciate any and all suggestions, thank you!
Here is the script I’ve been using for my bricks:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyBrick : MonoBehaviour
{
public int numberOfHits = 0;
public int maxHits;
public Sprite noHit;
public Sprite oneHit;
public SpriteRenderer brickSprite;
public int brickValue;
public Transform powerup;
public GameMaster gameMaster;
public Vector3[] maskLocations;
void Start()
{
brickSprite = GetComponent<SpriteRenderer>();
gameMaster.GetComponent<GameMaster>();
maskLocations = new Vector3[8];
maskLocations[0] = new Vector3(-7.5f, 1.75f, 0f);
maskLocations[1] = new Vector3(-4.5f, 0.75f, 0f);
maskLocations[2] = new Vector3(-1.5f, -0.25f, 0f);
maskLocations[3] = new Vector3(1.5f, -0.25f, 0f);
maskLocations[4] = new Vector3(7.5f, -0.25f, 0f);
maskLocations[5] = new Vector3(-4.5f, -0.25f, 0f);
maskLocations[6] = new Vector3(-7.5f, 0.75f, 0f);
maskLocations[7] = new Vector3(-7.5f, 3.75f, 0f);
}
void Update()
{
if (this.CompareTag("Mask") && numberOfHits >= 1 && numberOfHits < maxHits)
{
int i = Random.Range(0, maskLocations.Length);
transform.position = maskLocations*.position;*
}
}
private void OnCollisionEnter2D(Collision2D other)
{
numberOfHits++;
transform.GetComponent().sprite = oneHit;
if (this.transform.CompareTag(“Bricks”))
{
int randomChance = Random.Range(1, 101);
if (randomChance < 30)
{
Instantiate(powerup, this.transform.position, other.transform.rotation);
}
}
if (numberOfHits >= maxHits)
{
gameMaster.UpdateScore(+brickValue);
Destroy(this.gameObject);
}
}
}