- Home /
having a hadtime with script access
I am having a impossible time with this. I have a laser script that I have on three weapons that are all attached to the player. each weapon is a child of the parent. What I want them to do is to be able to access a script that keeps score. However I am not able to do that. I have tried to find tutorials online but they are impossible to understand. Please help. I have included both scripts below. The first is the laser script and the second is the score script.
using UnityEngine;
using System.Collections;
public class LaserbeamTrio : MonoBehaviour
{
MonoBehaviour score;
LineRenderer line;
public GameObject explosionA;
public AudioClip boom;
public GameObject explosionB;
public GameObject explosionC;
void Start ()
{
line = gameObject.GetComponent<LineRenderer>();
line.enabled = false;
}
void Update ()
{
if(Input.GetButtonDown("Fire1"))
{
StopCoroutine("FireLaser");
StartCoroutine("FireLaser", 0.25F);
audio.Play();
}
}
IEnumerator FireLaser(float lengthOfTime)
{
line.enabled = true;
var t = Time.time;
while(Input.GetButton("Fire1") && Time.time - t < lengthOfTime)
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
line.SetPosition(0, ray.origin);
if(Physics.Raycast(ray, out hit, 150))
{
line.SetPosition(1, hit.point);
if (hit.collider.tag == "Asteroid")
{
Instantiate(explosionA, hit.point, transform.rotation);
Destroy(hit.transform.gameObject);
audio.PlayOneShot(boom, 1F);
GetComponent("Score");
}
else if (hit.collider.tag == "Missile")
{
Instantiate(explosionB, hit.point, transform.rotation);
Destroy(hit.transform.gameObject);
audio.PlayOneShot(boom, 1F);
GetComponent("Score");
}
else if (hit.collider.tag == "Ufo")
{
Instantiate(explosionC, hit.point, transform.rotation);
Destroy(hit.transform.gameObject);
audio.PlayOneShot(boom, 1F);
GetComponent("Score");
}
}
else
line.SetPosition(1, ray.GetPoint(150));
yield return null;
}
line.enabled = false;
}
}
Score script
using UnityEngine;
using System.Collections;
public class Scorer : MonoBehaviour
{
private int count;
public GUIText countText;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
count =count +1;
SetCountText ();
}
void SetCountText ()
{
countText.text = "Score : " + count.ToString();
if(count >= 100)
{
Application.LoadLevel ("win level 1");
}
}
}
Answer by getyour411 · Mar 22, 2014 at 10:38 PM
Change
MonoBehaviour score;
to
public Score score;
in the Inspector, drag/drop the gameObject that has Score.cs attached to it in the slot. Once you've done that, you can reference all the variables and methods (public) in Score via score.myVar or score.count++, etc. So you probably don't want count in Update(), instead make it public and just increment it from the top script, or create a new method in bottom script like
public void IncrementCount(int _value)
{
count += _value;
}
and in your top script call score.IncrementCount(1)
Changed around $$anonymous$$onoBehaviour score; to public Score score; but got the error Assets/Standard Assets/Scripts/personal/LaserbeamTrio.cs(6,16): error CS0246: The type or namespace name Score' could not be found. Are you missing a using directive or an assembly reference? I also got the error Assets/Score.cs(6,21): error CS0501:
Score.IncrementCount(int)' must have a body because it is not marked abstract, extern, or partial How do I fix these errors?
Just noticed your Score.cs is Scorer so pretty much anywhere I said Score change it to Scorer (and score to scorer, that second error you posted is because it's in uppercase)