Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyShoot : MonoBehaviour
{
   
   public Transform rifle;
   public Transform player;
   public float distance; 
   
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.LookAt(player);
    }

    private void Shoot()
    {
        RaycastHit hit;

        PlayerHealth playerHealth;

        if(Physics.Raycast(transform.position, transform.forward, out hit, distance))
        {
            if(hit.transform.tag == "Player")
            {
                playerHealth = hit.transform.GetComponent<PlayerHealth>();
                playerHealth.TakeDamage();
                Debug.Log(playerHealth.health);
            }
        }
    }
}


What I have tried:

I have tried nothing becsause when I watched a video or looked it up, nothing helped.
Posted
Updated 16-Mar-22 3:20am

1 solution

I don't write C# but assume this has the same meaning as it would in C++. The problem is that TakeDamage is protected or private, so that Shoot can't access it. Change TakeDamage to public (or protected, if the class that defines Shoot is a subclass of the one that defines TakeDamage).
 
Share this answer
 
v2
Comments
Mark Kidder 16-Mar-22 19:56pm    
Thank you. I was reading that on a different paage, but couldn't figure out where on mine. Thank you.
Greg Utas 16-Mar-22 20:08pm    
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900