- Home /
Question by
Nzingha · Aug 05, 2014 at 02:32 AM ·
getcomponentscoreaddreferencing
Referencing Scripts properly and still not working? Help!
Okay so I need to fix the scoring for my game. I want score = (Time x 10)+(count x 10) and I am referencing count from finishline script and Time from Player script. I dont understand why It not working? smh please help!
using UnityEngine;
using System.Collections;
using UnityEditor;
public class Score : MonoBehaviour
{
public GUIText TheScore;
public float myScore;
//Script Reference
// public Clock myTimer;
// public Finishline count;
void Start()
{
GameObject Player = GameObject.Find ("Player");
Clock2 Timer = Player.GetComponent<Clock2> ();
GameObject finishline = GameObject.Find("finishline");
Finishline count = finishline.GetComponent<Finishline>();
}
public int Timestop = Clock2.Timer.myTimer*10;
public int Balls = Finishline.count*10;
void Update()
{
if (Time >= 300) //why is myTimer isnt working?
{
myScore = Timestop + Balls;
}
if (count = 12) //why is count isnt working?
{
myScore = Timestop + Balls;
}
}
}
Comment
Answer by meat5000 · Aug 05, 2014 at 02:43 AM
Declare variables in Class then perform Find and GetComponent in Start().
If you declare a variable in Start() it only has scope within that function.
Is count a variable of Finishline script? Your GetComponent is stored in a variable count of Type Finishline, so if count is a variable of Finishline you would have
count.count*10.
This is bad naming. Try
private Finishline finishLineScript;
private GameObject finishline;
void Start()
{
finishline = GameObject.Find("finishline");
finishLineScript = finishline.GetComponent<Finishline>();
//ETC
}
So,
Finishline.count*10;
becomes
finishLineScript.count*10