- Home /
need help wih score system
my player has two scripts to move, one is to move the horse and the other to move the hand for a spear, on this script i want to make my score adds when the spear collides with the gameobject.
in my project I have one gameobject, and in this gameobject I have four target, 2 object is 1 poin, 1 target is one point and last is 3 point, I need help with this.
this script I need put in hand because this script is for hand movement, in hand is spear when I put game objects in project and spear is collide nothing happens.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public Text countText;
private int count;
void start ()
{
count = 0;
SetCountText ();
}
void Update()
{
var z = Input.GetAxis("Mouse Y") * Time.deltaTime * 3.0f;
transform.Rotate(0, 0, z);
transform.Translate(0, 0, z);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
}
}
Answer by efeguclu · Jan 14, 2018 at 03:00 PM
First, check if you selected isTrigger in your pickup's collider component if you didn't, select it Then, if this script is attached to your hand you have to attach another script in your spear's inspector and add all of this script instead void Update() into another script replace void start() to void Start() I made script changes for you :
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class SpearController : MonoBehaviour
{
public Text countText;
private int count;
void Start ()
{
count = 0;
SetCountText ();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
}
}
and here is your handScript :
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour
{
void Update()
{
var z = Input.GetAxis("Mouse Y") * Time.deltaTime * 3.0f;
transform.Rotate(0, 0, z);
transform.Translate(0, 0, z);
}
}
Answer by unity_INzTEchDYxPHAQ · Jan 15, 2018 at 11:42 AM
Add a rigidbody on one of the interacting objects, the rigidbody must not be set to kinematic. I'd suggest putting it on the spear.
thanks work with rigidbody
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class SpearController : $$anonymous$$onoBehaviour
{
private Rigidbody rb;
public Text countText;
private int count;
void Start ()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
}
}
Your answer
