- Home /
Scoring System
Here's what I want to do: Character 1 comes onscreen which has a score/rating. Then Character 2 comes onscreen which also has a score/rating.
If Character 1 has a higher score/rating, play animation 'Victorious' and Character 2 play animation 'Defeat'. And vice-versa.
Anyone know how I would go about employing a score/rating system? Thanks
I've got this script which works in a very confined sense:
using UnityEngine;
using System.Collections;
public class WinLose : $$anonymous$$onoBehaviour {
public int Car1Speed = 196;
public int Car2Speed = 205;
private GameObject Win;
private GameObject Lose;
// Use this for initialization
void Start () {
Win = GameObject.Find ("Cube");
Lose = GameObject.Find ("Cylinder");
Win.SetActive(false);
Lose.SetActive(false);
}
// Update is called once per frame
void Update () {
if(Car1Speed > Car2Speed){
Win.SetActive (true);
Lose.SetActive(false);
}
else{
Win.SetActive(true);
Lose.SetActive(false);
}
}
}
However, the trouble is my $$anonymous$$d boggles when I think that I have to apply this to maybe 100 models and how I would go about defining them. I obviously can't do them all like the above as that a ridiculous amount of variations.
Is there any way I could do a script along these lines:
Each model has the same script saying find this models 'speed' and find the other models 'speed' (whichever model that may be). Then if this models number is bigger than the other, do something, and if not, do something else.
I feel I'm getting a little closer, I just need a point in the right direction on this.
What you need to do is type:
Unity polymorphism
In google. Top link should be the Learn tutorial. Also watch the one on Inheritance.
Again sorry for typos, on my phone.
Answer by uanmanarmy · Mar 05, 2015 at 11:56 AM
You could simple check it.
Grab the both scores and make a check and in that check place your animation call.
This is what I'm struggling with, not grabbing and checking the scores, but assigning them in the first place. I'm thinking numerical value so the script can check 'if number is above, trigger one animation, else trigger other animation'
Would it be safe to say you want a random number in range to assign to each character when they appear. If so look up Random.Range.
Say you want to assign a value between 5 and 20:
Random.Range (5,21);
The upper value is exclusive of using int so set it to 1 above the upper number.
Sorry for typos, on my phone.
Hey thanks for the reply. I don't want it to assign a random number. I want each character to have a specific value. For example, if a goon enemy comes onscreen, he would have a numerical value of 1. Whereas a boss enemy would have a value of 100. Thus the boss enemy would 'win' as 100 is a bigger number than 1. Any clues?
Well you can just set that in a script attached to the character or you could store it in an array and assign each character a number.
Is this just one value for each character or a lot of different values like strength, agility, defence skills etc?
And are these values likely to change over time?
PS something wrong with my account so Im not getting mail updates, so sorry if I appear to go quiet.
I was going to start off with just the one value but expand into multiple values. The idea that each player chooses a character then a category is chosen (i.e. speed) then whoever has the highest value wins.
Answer by hollym16 · Mar 09, 2015 at 03:59 PM
Ok, I've got this so far, which may help some people. I've got a script on 'Car2' like so:
using UnityEngine;
using System.Collections;
namespace Car{
public class WinLose1: MonoBehaviour {
public int Car2Speed = 205;
}
}
Then on 'Car1' I've got this script:
using UnityEngine;
using System.Collections;
using Car;
public class WinLose : MonoBehaviour {
public int Car1Speed = 196;
private GameObject Win;
private GameObject Lose;
public GameObject gameControllerObject;
public WinLose1 gameController;
void Start () {
Win = GameObject.Find ("Cube");
Lose = GameObject.Find ("Cylinder");
Win.SetActive(false);
Lose.SetActive(false);
gameControllerObject = GameObject.Find ("/ImageTarget2");
gameController = gameControllerObject.GetComponent<WinLose1>();
}
void Update () {
int NextCar = gameController.Car2Speed;
if(Car1Speed > NextCar){
Win.SetActive (true);
Lose.SetActive(false);
}
else{
Win.SetActive(false);
Lose.SetActive(true);
}
}
}
This accesses the other script and gets the int value which the determines whether to display the Cube or Cylinder (i.e. win or lose).
The next things I need to do are:
Make it only find the int value when the opponent is on screen. (OnTrackingFound?)
Make it reciprocal so they are looking for each others int values and displaying win or lose depending on each others values.
Alter the script so it's not just reading from one external script but whichever script is attached to the model that comes on screen.
This is a lot more complicated than I originally thought but I'm determined to crack it now!
Careful formatting that code, I've corrected it this time.
Why not have a prefab car, hold the Car speed in an Array. When you want the car on the screen instantiate it and pick up the car speed from the array.
Just check the values in the array.
So create a public int array for testing:
public int[] CarSpeed;
This is in a script attached to something in the scene. Also hold the currentCar number and the nextCar number in that script both as ints.
public int currentCar;
public int nextCar;
public Vector3 CurrentCar;
public Vector3 NewCar;
public GameObject carPrefab;
void Start()
{
GameObject newCar = (GameObject)Instantiate(carPrefab, CurrentCar, Quaternion.Identity);
currentCar = 0;
nextCar = 1;
newCar.GetComponent<$$anonymous$$yCarScript>().CarNumber = currentCar;
newCar.GetComponent<$$anonymous$$yCarScript>().CarSpeed = CarSpeed[currentCar];
}
When the game starts Instantiate the first car and have the prefab hold a script with the cars public int number (CarNumber) above and it's speed from that Array (CarSpeed) above;
Now I don't know how you want to create the next car but let's say you have a button you click that calls a function in the same script when you want a new car but uses the nextCar number and also increase that number by one. Place the car in the NewCar Vector3.
Typed without testing so there may be errors.
Your answer
![](https://koobas.hobune.stream/wayback/20220613185115im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Script does not work on version 4.6.0. 0 Answers
Character Aiming 1 Answer
How to handle multiple animation variants 1 Answer
import softimage mod tool 7.5 to unity, good or not? 0 Answers