I have the scorekeeper, and the scorekeeper script. do I attach it to the targets for the tin cup alley project?
Announcement
Collapse
No announcement yet.
Score Assistance
Collapse
X
-
It will be just like in Pachinko and Space Invaders. You can open those projects on a Mac or PC.
You'll need to have a ScoreKeeper gameObject that has a ScoreKeeperScript on it.
Each of the targets will need to have a script that probably has something like this in it:
Code:public GameObject scoreKeeperGO; public int myValue; void OnCollisionEnter (collider other) { scoreKeepterGO.GetComponent<ScoreKeeperSciprt>().ChangeScore(myValue); }
-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TargetScript : MonoBehaviour
{
public int myValue;
public GameObject Scorekeeper;
private void OnCollisionEnter(Collision collision)
{
iTween.RotateTo(transform.parent.gameObject, new Vector3(90f, 0f, 0f), .5f);
}
IEnumerator StandUp()
{
float randomWaitTime;
randomWaitTime = Random.Range(1f, 3f);
yield return new WaitForSeconds(randomWaitTime);
iTween.RotateTo(transform.parent.gameObject, new Vector3(0f, 0f, 0f), 2f);
}
void OnCollisionEnter(Collider Other)
{
Scorekeeper.GetComponent<ScoreKeeperScript>().Chan geScore(myValue);
}
}
The first one is the Target script, here is the scorekeeper script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreKeeperScript : MonoBehaviour
{
public int score;
public Text scoreUI;
public void ChangeScore(int sentValue)
{
score = score + sentValue;
GameObject.Find("ScoreUI").GetComponent<Text>().te xt = score.ToString();
}
}
Comment
-
Please, please, please use the # button and then paste your code within the ["code"] and ["/code"] tabs. Really tough to read code this way.
A few things:
1. Your TargetScript has two OnCollisionEnter functions. It should have only one (with both your iTween and ScoreKeeper.GetComponent work in it).
2. There are a few weird spaces in there "Chan geScore" not sure if that's just a copy/paste error or if thats really in your code. If it is you need to fix that.
3. Make sure that on all your targets that you've actually changed the score from "0".
Other than that, the code looks reasonable. When you play the game I'm betting that the console shows an error or two. What are those errors?
Comment
Comment