Hi... I'm new to C# and I am trying to click on a barrel GameObject and then have it be destroyed and replaced with an explosion GameObject.
All of this works except when I click on the barrel, it is destroyed and it is the PLAYER Object that the Explosion GameObject is instantiated on...
If anyone could look at this code and see what is causing this problem I would really appreciate it... I have it narrowed down to the one line in asterisks that's causing the problem... ` using UnityEngine; using System.Collections;
public class PlayerScript : MonoBehaviour {
public GameObject explosion;
public GameObject gun;
public int score=0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (gun.active)
{
if (Input.GetMouseButtonDown(0)){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit=new RaycastHit();
if (Physics.Raycast(ray,out hit))
{
if (hit.collider.gameObject.name=="barrel")
{
Debug.Log("Hit the barrel");
**Instantiate (explosion, collider.gameObject.transform.position, collider.gameObject.transform.rotation);**
Destroy(hit.collider.gameObject, 3);
score++;
}
}
}
}
}
void OnGUI()
{
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.gameObject.name=="machineGun")
{
Debug.Log("Object picked up "+hit.gameObject.name);
Destroy(hit.gameObject);
gun.active=true;
}
}
}`