Issues w/ 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 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);
}
}
}
}
}
}
using UnityEngine;
public class ZScore : $$anonymous$$onoBehaviour {
public int Score;
private void DeductPoints (int DamageAmount) {
Debug.Log("CurrentScore += " + Score);
GlobalScore.CurrentScore += Score;
}
}
using UnityEngine;
public class HandGunDamage : $$anonymous$$onoBehaviour {
public int DamageAmount = 5;
public float TargetDistance;
public float AllowedRange = 20.0f;
void Update()
{
if (GlobalAmmo.LoadedAmmo >= 1)
{
if (Input.GetButtonDown("Fire1"))
{
RaycastHit Shot;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
{
Debug.Log("Raycast hit");
var score = Shot.transform.GetComponent<ZScore>();
if (score != null)
TargetDistance = Shot.distance;
if (TargetDistance < AllowedRange)
{
Debug.Log ("Hit ZScore component");
Shot.transform.Send$$anonymous$$essage("DeductPoints", DamageAmount, Send$$anonymous$$essageOptions.DontRequireReceiver);
}
}
}
}
}
You should not use Send$$anonymous$$essage
Debug.Log("Raycast hit :" + Shot.transform.name );
var score = Shot.transform.GetComponent<ZScore>();
if( score != null && raycastHit.distance < AllowedRange )
{
TargetDistance = raycastHit.distance ;
Debug.Log ("Hit ZScore component");
score.DeductPoints();
}
Answer by Hellium · Dec 17, 2017 at 06:41 PM
Here is how I you have handled it:
// 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?
Yes, my bad, you have to put public
here :
public void DeductPoints()
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
Your scene should be similar to this :
GlobalScore (empty) with the GlobalScore component referencing a Text under a canvas
Character with the script used to move and rotate
→ Weapon (I guess?) with the HandGunDamage script
Canvas
→ Text, referenced by GlobalScore
Target
→ Target25 with the Target script
→ Target50 with the Target script
→ Target100 with the Target script
$$anonymous$$ake sure HandGunDamage works correctly by adding various Debug.Log
in the conditions to check whether the conditions are satisfied or not.
$$anonymous$$y steps: 1. Created new UI-Text -- > added "ScoreLabel" witch child "ScoreNumber" 2. Created empty GameObject "Score$$anonymous$$eeper". 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 $$anonymous$$esh 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!
Can you add some Debug.Log to check whether the functions are correctly called?
// HandGunDamage.cs
// Have I hit a target?
Target target = raycastHit.transform.GetComponent<Target>();
Debug.Log(raycastHit.transform.name + " has been it. " + raycastHit.distance + "/" + AllowedRange );
if( target != null && raycastHit.distance < AllowedRange )
{
Debug.Log( "Target hit!" );
// Deduct the points of the hit target
targetDistance = raycastHit.distance ;
target.DeductPoints();
}
// Target.cs
void DeductPoints()
{
Debug.Log("Adding " + points + " points");
GlobalScore.CurrentScore += points;
}
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? $$anonymous$$any thanks /C
Well, it's hard to diagnose. It seems the target is detected correctly (according to the Hit ZScore component
message). Sorry to ask this to you, but, could you provide the last version of the HandGunDamage
and ZScore
scripts please?
Please, post the scripts as comments (by hitting the button "Add comment" below your question)
Answer by cha_barnekow · Dec 18, 2017 at 08:52 PM
I've added them. I couldn't kill my other gameobjects if I used the HandGunDamage you kindly previously provided
Your answer
Follow this Question
Related Questions
How to fix snake behavior in Unity3d? 0 Answers
How can I make this character move smoothly? 1 Answer
Navigation in VR Mode?!! 0 Answers
Unity3d Character Spawn Point 0 Answers