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 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);
}
}
}
}
}
// GlobalScore.cs
// Attach this script to your empty gameobject called GlobalScore
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GlobalScore : MonoBehaviour
{
// Drag & Drop the gameobject with the Text component
[SerializeField]
private Text scoreText;
private int currentScore;
public int CurrentScore
{
get { return currentScore; }
set
{
currentScore = value ;
scoreText.text = currentScore.ToString();
}
}
}
// Target.cs
// Attach this script to the objects you can shoot at
using UnityEngine;
public class Target : MonoBehaviour
{
// Drag & Drop the GlobalScore gameobject in the inspector
[SerializeField]
private GlobalScore GlobalScore;
// Specify the number of points added to the score when the target is hit
[SerializeField]
private int points;
void DeductPoints()
{
GlobalScore.CurrentScore += points;
}
}
// HandGunDamage.cs
using UnityEngine;
public class HandGunDamage : MonoBehaviour
{
[SerializeField]
private float AllowedRange = 15.0f;
private float targetDistance;
void Update()
{
// Can I shoot?
if ( GlobalAmmo.LoadedAmmo >= 1 && Input.GetButtonDown( "Fire1" ))
{
// Have I hit somehting?
RaycastHit raycastHit;
if ( Physics.Raycast( transform.position, transform.forward, out raycastHit ) )
{
// Have I hit a target?
Target target = raycastHit.transform.GetComponent<Target>();
if( target != null && raycastHit.distance < AllowedRange )
{
// Deduct the points of the hit target
targetDistance = raycastHit.distance ;
target.DeductPoints();
}
}
}
}
}
@Hellium thank you so much for helping!! been sitting with this all day
I’ve followed your instructions but get the following error message reg. HandGunDamage script: “Assets/Scripts/HandGunDamage.cs(26,28): error CS0122: `Target.DeductPoints()’ is inaccessible due to its protection level”. Should I add ‘public’ somewhere?
gaah it still doesn’t work. I will delete all objects and try to do it from scratch again. I’m making an FPS and there isn’t a problem with creating and enemy object and destroying (shooting) it but adding the points and display of the scores seems impossible at the moment. I’ve probably been sitting with it to long. Thank you so much for taking the time to help though
My steps: 1. Created new UI-Text – > added “ScoreLabel” witch child “ScoreNumber”
2. Created empty GameObject “ScoreKeeper”. Added “Global Script” above and dragged “ScoreNumber” into “Score Text” 3. Created three 3D Cylinder GameObjects. Re-shaped so the looked like a dartboard. Deleted Box Collider, added Mesh Collider 4. Created “ZSCore” script and dragged these to the different Cylinder GameObjects. I do have an enemy script attached to other game objects which works fine when I shoot at it (they’re not connected with points and text/scoring system). Should I add that code to my main post? Thank you so, so much again for your help!
I received these messages: Hit ZScore component UnityEngine.Debug:Log(Object)
HandGunDamage:Update() (at Assets/Scripts/HandGunDamage.cs:29). Raycast hit
UnityEngine.Debug:Log(Object)HandGunDamage:Update() (at Assets/Scripts/HandGunDamage.cs:23). Do you know what they mean? Many thanks /C
I received these messages: Hit ZScore component UnityEngine.Debug:Log(Object)
HandGunDamage:Update() (at Assets/Scripts/HandGunDamage.cs:29). Raycast hit
UnityEngine.Debug:Log(Object)HandGunDamage:Update() (at Assets/Scripts/HandGunDamage.cs:23). Do you know what they mean? Many thanks /C