Unexpected behaviour from script

I tried to make a script to make a crate shatter, it worked;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DestructibleCrate : MonoBehaviour
{

    public GameObject destroyedversion;

    private void OnMouseDown()
    {
        Instantiate(destroyedversion, transform.position, transform.rotation);
        Destroy(gameObject);
    }

}

but it only worked when I clicked on it I wanted it to have health and destroy it with my gun
so I combined my script to make a cube disappear when its shot but it did not work plz help me
script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class destruction : MonoBehaviour {

    public float health = 50f;
    public GameObject destroyedversion;

    public void takedamage(float amount)
    {
        health -= amount;
        if (health <= 0f)
        {
            die();
        }
    }
    void die()
    {
        Instantiate(destroyedversion, transform.position, transform.rotation);
        Destroy(gameObject);
    }
}

ps. there are no errors

Where is the takedamage method called?

its called in my gun script

using UnityEngine;

public class Gun : MonoBehaviour {

    public float damage = 10f;
    public float range = 100f;
    public float firerate = 15f;
    public float impactforce = 30f;

    public Camera fpscam;
    public ParticleSystem muzleflash;
    public GameObject impacteffect;

    private float nexttimetofire = 0f;
	
	// Update is called once per frame
	void Update () {

        if (Input.GetButton("Fire1") && Time.time >= nexttimetofire)
        {
            nexttimetofire = Time.time + 1f / firerate;
            shoot();
        }
		
	}

    void shoot ()
    {
        muzleflash.Play();

        RaycastHit hit;
        if (Physics.Raycast(fpscam.transform.position, fpscam.transform.forward, out hit, range))
        {
            Debug.Log(hit.transform.name);

            Target  target = hit.transform.GetComponent<Target>();
            if (target != null)
            {
                target.takedamage(damage);
            }

            if (hit.rigidbody != null)
            {
                hit.rigidbody.AddForce(-hit.normal * impactforce);
            }

            GameObject impactgo = Instantiate(impacteffect, hit.point, Quaternion.LookRotation(hit.normal));
            Destroy(impactgo, 2f);
        }
    }
}

at least i think

i am stil a noob at C#

Please use code formatting when copy/pasting your code into the forum, see the link below for details. :slight_smile:


See also;

Does your cube have a Target script attached?

Your code is calling target.takedamage, does your target class subsequently reference the destruction class, and call takedamage in there?

OOH i it is only looking for target not for destruction thank you

1 Like

No worries. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms