Method inaccessible due to its protection level - c#

I'm currently developing within Unity 2018 and have made a script for decreasing a character's health on collision with an enemy:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthManager : MonoBehaviour
{
public static int currentHealth;
public Slider healthBar;
void Awake()
{
healthBar = GetComponent<Slider> ();
currentHealth = 100;
}
void ReduceHealth()
{
currentHealth = currentHealth - 1;
healthBar.value = currentHealth;
}
void Update()
{
healthBar.value = currentHealth;
}
}
When I try to use said method in the scripting file for the enemy I get an error stating "Assets/Custom Scripts/BeetleScript.cs(46,28): error CS0122: `HealthManager.ReduceHealth()' is inaccessible due to its protection level"
The following is the enemy script initiating the variables being used and calling the method:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BeetleScript : MonoBehaviour
{
Animator animator;
public GameObject cucumberToDestroy;
public bool cherryHit = false;
public float smoothTime = 3.0f;
public Vector3 smoothVelocity = Vector3.zero;
public PointsManager _ptsManager;
public HealthManager _healthManager;
void Start()
{
animator = GetComponent<Animator>();
}
void Update()
{
if (cherryHit)
{
var cm = GameObject.Find("CucumberMan");
var tf = cm.transform;
this.gameObject.transform.LookAt(tf);
// move towards Cucumber Man
animator.Play("Standing Run");
transform.position = Vector3.SmoothDamp(transform.position, tf.position,
ref smoothVelocity, smoothTime);
}
}
// Collision Detection Test
void OnCollisionEnter(Collision col)
{
if (col.gameObject.CompareTag("Player"))
{
_healthManager = GameObject.Find
("Health_Slider").GetComponent<HealthManager>();
_healthManager.ReduceHealth();
if (!cherryHit)
{
BeetlePatrol.isAttacking = true;
var cm = GameObject.Find("CucumberMan");
var tf = cm.transform;
this.gameObject.transform.LookAt(tf);
animator.Play("Attacking on Ground");
StartCoroutine("DestroySelfOnGround");
}
else
{
animator.Play("Standing Attack");
StartCoroutine("DestroySelfStanding");
}
}
}
}
Any help to fix this would be appreciated.

Your methods are private.
You have to write public in front of the method you want to access from outside the class.
public void ReduceHealth()
{
...
}

You need to make void ReduceHealth() to be public -> public void ReduceHealth()

Related

NavmeshAgent rotastes 90 degres when stoped

A few days ago I started learning unity, I implemented a very simple navMeshAgent so when I click on the map if the unit IsLeader == true (has de focus) de agent of the unit will move to de position, the problem is that when the unit stops moving it rotates 90 degrees
Here you have the unit class, LeaderMovement class and LeaderManager class.
Unit:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.AI;
public class Unit : MonoBehaviour
{
private NavMeshAgent agent;
private Animator animator;
public bool isLeader;
void Start()
{
agent = Agent;
isLeader = false;
animator = Animator;
}
// Update is called once per frame
void Update()
{
}
public bool IsLeader
{
get;
set;
}
public NavMeshAgent Agent
{
get;
set;
}
public Animator Animator
{
get;
set;
}
}
LeaderMovement
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class LeaderMovement : MonoBehaviour
{
RaycastHit hitInfo = new RaycastHit();
private Unit unit;
private Animator animator;
private void Awake()
{
unit = GetComponent<Unit>();
unit.Agent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(1))
{
ClickToMove();
}
if (Vector3.Distance(unit.Agent.destination, transform.position) <= unit.Agent.stoppingDistance )
{
animator.SetBool("isMoving", false);
}
}
public void ClickToMove()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction, out hitInfo))
{
SetDestination(hitInfo.point);
}
}
private void SetDestination(Vector3 target)
{
if (unit.IsLeader==true) {
animator.SetBool("isMoving", true);
unit.Agent.SetDestination(target);
}
}
}
TeamManager
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TeamManager : MonoBehaviour
{
//control of the team
[SerializeField] private Unit leader;
[SerializeField] private Unit pastleader;
[SerializeField] private List<Unit> playerList;
int index;
// Start is called before the first frame update
void Start()
{
index = 0;
leader= playerList[index];
leader.IsLeader = true;
}
public void changeLeader()
{
try
{
index += 1;
pastleader = leader;
pastleader.IsLeader = false;
leader = playerList[index];
leader.IsLeader = true;
}
catch (ArgumentOutOfRangeException)
{
pastleader = leader;
pastleader.IsLeader = false;
index = 0;
leader = playerList[index];
leader.IsLeader = true;
}
}
public Unit getLeader()
{
return leader;
}
}
Im thankful to any help i can get

OnTriggerExit2D getting called unnecessarily

I hope you all are doing well. I have been following a Unity tutorial for a rhythm game and I have found this bug that I could not get past. Essentially, my OnTriggerExit2D is getting called too early. I'll include a picture in the conclusion of this post. I have tried logging the game object and it seems that all of my button objects suffer the same fate. I have included a link of the tutorial that I have been following in the conclusion. Any help towards figuring this out would be helpful.
Tutorial Link: https://www.youtube.com/watch?v=PMfhS-kEvc0&ab_channel=gamesplusjames
What my game looks like, the missed shows up when I've hit it.
Debug Output
GameManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public AudioSource theMusic;
public bool startPlaying;
public BeatScroller theBS;
public static GameManager instance;
public int currentScore;
public int scorePerNote = 100;
public int scorePerGoodNote = 125;
public int scorePerPerfectNote = 150;
public int currentMultiplier;
public int multiplierTracker;
public int [] multiplierTresholds;
public Text scoreText;
public Text multiText;
// Start is called before the first frame update
void Start()
{
instance = this;
scoreText.text = "Score: 0";
multiText.text = "Multiplier: x1";
currentMultiplier = 1;
}
// Update is called once per frame
void Update()
{
if(!startPlaying){
if(Input.anyKeyDown){
startPlaying = true;
theBS.hasStarted = true;
theMusic.Play();
}
}
}
public void NoteHit(){
Debug.Log("Note Hit On Time");
if(currentMultiplier-1 < multiplierTresholds.Length){
multiplierTracker++;
if(multiplierTresholds[currentMultiplier-1] <= multiplierTracker){
multiplierTracker = 0;
currentMultiplier++;
}
}
multiText.text = "Multiplier: x"+currentMultiplier;
//currentScore += scorePerNote * currentMultiplier;
scoreText.text = "Score: "+currentScore;
}
public void NormalHit(){
currentScore += scorePerNote * currentMultiplier;
NoteHit();
}
public void GoodHit(){
currentScore += scorePerGoodNote * currentMultiplier;
NoteHit();
}
public void PerfectHit(){
currentScore += scorePerPerfectNote * currentMultiplier;
NoteHit();
}
public void NoteMissed(){
Debug.Log("MISSED!");
multiText.text = "Multiplier: x1";
}
}
BeatScroller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BeatScroller : MonoBehaviour
{
public float beatTempo;
public bool hasStarted;
// Start is called before the first frame update
void Start()
{
beatTempo = beatTempo / 60f;
}
// Update is called once per frame
void Update()
{
if(!hasStarted){
}else{
transform.position -= new Vector3(0f, beatTempo*Time.deltaTime, 0f);
}
}
}
ButtonController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtonController : MonoBehaviour
{
// Start is called before the first frame update
private SpriteRenderer theSR;
public Sprite defaultImage;
public Sprite pressedImage;
public KeyCode keyToPress;
void Start()
{
theSR = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(keyToPress))
{
theSR.sprite = pressedImage;
}
if(Input.GetKeyUp(keyToPress))
{
theSR.sprite = defaultImage;
}
}
}
noteObject
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class noteObject : MonoBehaviour
{
public bool canBePressed;
public KeyCode KeyToPress;
public GameObject hitEffect, goodEffect, perfectEffect, missedEffect;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyToPress))
{
if(canBePressed)
{
gameObject.SetActive(false);
if(Mathf.Abs(transform.position.y) > 0.25){
GameManager.instance.NormalHit();
Debug.Log("Normal Hit!");
Instantiate(hitEffect,transform.position, hitEffect.transform.rotation);
}else if(Mathf.Abs(transform.position.y) > 0.05f){
GameManager.instance.GoodHit();
Debug.Log("Good Hit!!");
Instantiate(goodEffect,transform.position, goodEffect.transform.rotation);
}else{
GameManager.instance.PerfectHit();
Debug.Log("PERFECT HIT!!!");
Instantiate(perfectEffect,transform.position, perfectEffect.transform.rotation);
}
}
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Activator")
{
canBePressed = true;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if(other.tag == "Activator")
{
Debug.Log("Exited collider on game object: "+ other.gameObject.name);
canBePressed = false;
GameManager.instance.NoteMissed();
Instantiate(missedEffect,transform.position, missedEffect.transform.rotation);
}
}
}
EffectObject
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EffectObject : MonoBehaviour
{
public float lifeTime = 1f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Destroy(gameObject, lifeTime);
}
}
Hmm ... You say that OnTriggerExit2D is called to early? I assume it's called when the elements are still inside one another? If that's the case I guess your bounding boxes don't have the right size, or the right shape. I see arrows, do they have a rectangular bounding box or a polygon one that follows their shape? Are all your bounding boxes the right size?
I figured out what was wrong thanks to AdrAs's comment.
Turns out I had to check the y position of my arrow and collider were well enough below the height of my button box collider. In addition to that, I reshaped my colliders. I found that this code did the trick for me. Thank You All For the Nudges in the right direction.
noteObject -> new OnTriggerExit2D
private void OnTriggerExit2D(Collider2D other)
{
if(other.tag == "Activator" && transform.position.y < -0.32)
{
Debug.Log("Exited collider on game object: "+ other.gameObject.name);
canBePressed = false;
GameManager.instance.NoteMissed();
Instantiate(missedEffect,transform.position, missedEffect.transform.rotation);
}
}

Particle System not working on multiple enemies

I have a particle system for when the enemy is destroyed. I have multiple enemies coming from the same path (using the same prefab) and the particle system is only working on the first enemy. Can someone tell me why it is not working on the others as well? Thank you.
EnemyShooting scrip (this is where the piece of code is for the explosion):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyShooting : MonoBehaviour {
[SerializeField] float EnemyLaserSpeed = 10f;
[SerializeField] float EnemyLaserFireTime;
[SerializeField] GameObject LaserBulletEnemyPreFab;
[SerializeField] int MaxNumberOfHits = 1;
public Transform explosion;
int CurrentNumberOfHits = 0;
Coroutine FireCoroutine;
void OnTriggerEnter2D(Collider2D collider)
{
if(collider.gameObject.tag == "PlayerLaser")
{
if (CurrentNumberOfHits < MaxNumberOfHits)
{
CurrentNumberOfHits++;
Destroy(collider.gameObject);
Score.ScoreValue += 2;//The user will be rewarded 1 point
}
if (explosion)//EXPLOSION CODE
{
GameObject exploder = ((Transform)Instantiate(explosion, this.transform.position, this.transform.rotation)).gameObject;
Destroy(exploder, 2.0f);
}
}
}
void DestroyEnemy()
{
if(CurrentNumberOfHits >= MaxNumberOfHits)
{
Destroy(gameObject);
EnemySpawner.Instance.OnEnemyDeath(); // Tell the EnemySpawner that someone died
}
}
private void Fire()
{
FireCoroutine = StartCoroutine(ShootContinuously());
}
void BecomeVisible()
{
Fire();
}
IEnumerator ShootContinuously()
{
while (true)
{
GameObject LaserBulletEnemy = Instantiate(LaserBulletEnemyPreFab, this.transform.position, Quaternion.identity) as GameObject;
LaserBulletEnemy.GetComponent<Rigidbody2D>().velocity = new Vector2(0, EnemyLaserSpeed);
EnemyLaserFireTime = Random.Range(0.5f, 0.9f);
yield return new WaitForSeconds(EnemyLaserFireTime);
}
}
// Use this for initialization
void Start () {
BecomeVisible();
}
// Update is called once per frame
void Update () {
DestroyEnemy();
}
}
EnemySpawner : (I thought this script might help in a way so I attached it)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemySpawner : MonoBehaviour
{
[SerializeField] GameObject EnemyPreFab;
[SerializeField] int MaxEnemies = 30;
[SerializeField] float EnemySpawnTime = 1.00001f;
[SerializeField] GameObject FirstWaypoint;
int CurrentNumOfEnemies = 0;
public int EnemiesToNextLevel = 7;
public int KilledEnemies = 0;
public LevelManager myLevelManager;
public static EnemySpawner Instance = null;
int timesEnemyHit;
IEnumerator SpawningEnemies()
{
while (CurrentNumOfEnemies <= MaxEnemies)
{
GameObject Enemy = Instantiate(EnemyPreFab, this.transform.position, Quaternion.identity);
CurrentNumOfEnemies++;
yield return new WaitForSeconds(EnemySpawnTime);
}
}
void Start()
{
if (Instance == null)
Instance = this;
StartCoroutine(SpawningEnemies());
timesEnemyHit = 0;
if (this.gameObject.tag == "EnemyHit")
{
CurrentNumOfEnemies++;
}
}
public void OnEnemyDeath()
{
CurrentNumOfEnemies--;
/*
if (CurrentNumOfEnemies < 5)
{
// You killed everyone, change scene:
LaserLevelManager.LoadLevel("NextLevelMenu");
}
*/
KilledEnemies++;
if (KilledEnemies >= EnemiesToNextLevel)
{
LaserLevelManager.LoadLevel("NextLevelMenu");
}
}
}

Creating a target system

I'm making a target in Unity that looks like a dartboard with three different levels of scoring depending on where you shoot. The issue is that the Score Text wont change when I shoot on the target. I'm a novice and I "translated" below code from Javascript and wondering if you experts could see
if there is any issues with the code?
GlobalScore (attached this to an empty gameObject. I draged the text 'ScoreNumber' to ScoreText slot in Unity)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GlobalScore : MonoBehaviour {
public static int CurrentScore;
public int InternalScore;
public GameObject ScoreText;
void Update () {
InternalScore = CurrentScore;
ScoreText.GetComponent<Text>().text = "" + InternalScore;
}
}
ZScore25 (created 3 scripts (ZScore25, ZScore50, ZScore100) which I attached to the three cylinder gameObject I created)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ZScore25 : MonoBehaviour
{
void DeductPoints(int DamageAmount)
{
GlobalScore.CurrentScore += 25;
}
}
HandgunDamage Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HandGunDamage : MonoBehaviour {
public int DamageAmount = 5;
public float TargetDistance;
public float AllowedRange = 15.0f;
void Update () {
if (GlobalAmmo.LoadedAmmo >= 1) {
if (Input.GetButtonDown("Fire1"))
{
RaycastHit Shot;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
{
TargetDistance = Shot.distance;
if (TargetDistance < AllowedRange)
{
Shot.transform.SendMessage("DeductPoints", DamageAmount, SendMessageOptions.DontRequireReceiver);
}
}
}
}
}
}
Debug.Log (or breakpoints) help you to see where the problem lies. I tested your case with minor changes and the score text value was changing.
I replaced ZScore25,50,100 scripts with a single ZScore script that has score as public field which you can set in the editor.
public class ZScore : MonoBehaviour {
public int Score;
public void DeductPoints() {
Debug.Log("CurrentScore += " + Score);
GlobalScore.CurrentScore += Score;
}
}
..and then I used raycast (the example script below was attached to camera):
void Update () {
if (Input.GetMouseButtonDown(0)) {
var lookAtPos = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane));
transform.LookAt (lookAtPos);
RaycastHit Shot;
if (Physics.Raycast(transform.position, transform.forward, out Shot))
{
Debug.Log ("Raycast hit");
var score = Shot.transform.GetComponent<ZScore> ();
if (score != null) {
Debug.Log ("Hit ZScore component");
score.DeductPoints ();
}
}
}
}

How can I use behaviours in Unity?

I am very new to c# and coding in general.
What I got is a weapon script that has a public int of 50 (damage). Then I got another script which is the enemy health.
Now what I want to do is use the value in the weapon script to apply it to the enemy health script and I have no clue how to do it.
I know its something quite simple but ive been bashing my head against the wall trying to figure this thing out.
Please help!
Weapon.cs:
using UnityEngine;
using System.Collections;
public class Weapon : MonoBehaviour {
static Animator anim;
public GameObject hitbox;
public int damage = 50;
private AudioSource MyAudioSource;
private AudioClip WeaponSound;
void Start () {
anim = GetComponentInParent<Animator>();
MyAudioSource = GetComponent<AudioSource>();
GetComponent<EnemyHealth>().TakeDamage(damage);
}
void Update () {
attack();
block();
}
public void attack() {
if (Input.GetButtonDown("Fire1")) {
GetComponent<EnemyHealth>().TakeDamage(damage);
anim.SetBool("IsAttacking", true);
hitbox.SetActive(true);
Debug.Log("hit");
MyAudioSource.PlayOneShot(WeaponSound);
}
else {
anim.SetBool("IsAttacking", false);
hitbox.SetActive(false);
}
}
public void block() {
if (Input.GetButtonDown("Fire2")) {
anim.SetBool("IsBlocking", true);
}
else {
anim.SetBool("IsBlocking", false);
}
}
}
EnemyHealth.cs:
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
public int maxHealth = 100;
private int currentHealth;
private Animator animator;
void Start () {
currentHealth = maxHealth;
animator = GetComponent<Animator>();
}
public void OnTriggerEnter(Collider other) {
other.GetComponent<Weapon>().attack();
}
public void TakeDamage(int _damage) {
currentHealth -= _damage;
animator.SetTrigger("IsHit");
if(currentHealth <= 0) {
Die();
}
}
void Die() {
animator.SetBool("Isdead", true);
Destroy(gameObject);
}
}
Assuming these are both instantiated in another main class (meaning ones not isntatiate from another) in c# you just use the '.' operator to access public elements, properties and functions in a class
main()
{
EnemyHealth myehlth = new EnemyHealth();
Weapon myweapn = new Weapon ();
myehlth.TakeDamage(myweapn.damage);
}
Here I used the '.' operator to access the public damage in your weapon class and then used the '.' oeprator to pass it to the public TakeDamage function in your health class
The answer was quite simple! Thanks noone392
main()
{
Weapon myweapn = new Weapon ();
TakeDamage(myweapn.damage);
}

Categories