I have three Scripts
Script One
Game Manager:
SIMPLY(Guitar Hero Like Game)
4 Tubes, 4 Spawnners and 4 receivers
Each tube has an Inactive atom parent over the Spawnners which has a variable spawnner and receiver each assigned according to their position. So Parent 1 is assigned receiver 1 and spawnner 1 through inspector, atom parent 2 is assigned receiver 2 spanner 2 etc.
So…
Script 1 Game Manager (Outside of everything, randomly picks a spawnner and instantiates the atom parents( all 4 atom parents spawnners and receivers are in a list)
GameObject atom1_child = Instantiate(atomReference[0], atomSpawnners[0].transform.position, atomSpawnners[0].transform.rotation) as GameObject;
Script Two
attached to the child gameObject(atoms)
when it spawns… and collides, in my case gets within distance with the base or what I call the receiver…
buttonOccupied = true; // Has a boolean to make sure atom is inside receiever
receiver.GetComponent().SA_Atom = gameObject; //Script 3 in receiver
//Script 3 is a controller with the variable SA_Atom
So when this object is at a certain distance from base or ITS receiver, it self assigns itself to script 3.
Script 3
When I ray cast to a specific receiver
if (hit.collider.gameObject && SA_Atom.GetComponent().buttonOccupied == true)
//If I hit the receiever…
{
Destroy(SA_Atom);
}
Heres the issue, ALL atom childs are being destroyed not just one. I tried several solutions, I cant wrap my head around it
ALSO, it doesnt destroy all atomChilds when the boolean is false, but when boolean is true and I click the receiver, it destroys all. I need it to be that ONE Specific Self assigned atom
HERES THE CODE
//Script ONE outside of anything
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class script_GameManager : MonoBehaviour
{
public GameObject[] atomReference = new GameObject[4];
private float spawnTimer;
public float setTimer;
public GameObject[] atomSpawnners = new GameObject[4];
private int randomSpawnnner;
void Start()
{
spawnTimer = setTimer;
}
void Update ()
{
timerUpdate();
}
void timerUpdate()
{
spawnTimer -= Time.deltaTime;
if(spawnTimer <= 0)
{
spawnTimer = setTimer;
randomSpawnnner = Random.Range(0, 4);
atomSpawn();
}
}
void atomSpawn()
{
switch (randomSpawnnner)
{
case 0:
GameObject atom1_child = Instantiate(atomReference[0], atomSpawnners[0].transform.position, atomSpawnners[0].transform.rotation) as GameObject;
atom1_child.SetActive (true);
break;
case 1:
GameObject atom2_child = Instantiate(atomReference[1], atomSpawnners[1].transform.position, atomSpawnners[1].transform.rotation) as GameObject;
atom2_child.SetActive (true);
break;
case 2:
GameObject atom3_child = Instantiate(atomReference[2], atomSpawnners[2].transform.position, atomSpawnners[2].transform.rotation) as GameObject;
atom3_child.SetActive (true);
break;
case 3:
GameObject atom4_child = Instantiate(atomReference[3], atomSpawnners[3].transform.position, atomSpawnners[3].transform.rotation) as GameObject;
atom4_child.SetActive (true);
break;
}
}
}
//Script 2 inside parents or atoms
using UnityEngine;
using System.Collections;
public class script_Atom : MonoBehaviour {
public Transform receiver;
private float receiverDistance;
public float atomSpeed;
private Transform targetReceiver;
public GameObject buttonParticle;
public bool buttonOccupied = false;
void Update()
{
//---- atom positionining and particle activation ----
targetReceiver = receiver.transform;
//easy reference to the vector 3 conversion of the receiver
receiverDistance = Vector3.Distance(transform.position, targetReceiver.transform.position);
//converts the vector 3 (x,y,z) distance into a float
if(receiverDistance > 0.1)
{
atomMove();
//atom will move if far from the zero, otherwise, stop
}//EO IF
if (receiverDistance <= 0.1 && receiverDistance >= -0.1)
{
buttonOccupied = true;
//button is inside receiver
this.receiver.GetComponent<script_buttonController>().SA_Atom = gameObject;
buttonParticle.SetActive(true);
//if atom is close to zero, activate particle system, other wise will be shut off by controller
//a new spawning atom may overrde it being true to false even if the "zero" is occupied
}
}
void atomMove()
{
transform.Translate(Vector3.down * atomSpeed * Time.deltaTime);
}
public void atomSend()
{
//Destroy(gameObject);
}
}
//script 3, attached to receivers
using UnityEngine;
using System.Collections;
public class script_buttonController : MonoBehaviour
{
RaycastHit hit;
public GameObject SA_Atom;
void Update()
{
if (SA_Atom)
{
}
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
//sets up ray cast to camera for controller
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast (ray, out hit, Mathf.Infinity))
{
if (SA_Atom)
{
if (hit.collider.gameObject && SA_Atom.GetComponent<script_Atom>().buttonOccupied == true)
{
SA_Atom.GetComponent<script_Atom>().buttonParticle.SetActive(false);
Destroy(SA_Atom);
}
}
else if(SA_Atom == null)
{
//Do Nothing, receive no errors, AFTER Destroyed Gets a NULL ERROR
}
}
}
}
}