Okay, I 100% know, this has been answered before and I have looked at about 20 other answers - and I’m so sorry to be asking it, but I just can’t seem to make it work in my script…
I have two scripts I’m trying to access, and write to, via gameObject.GetComponent but no matter how many times I change it, the compiler keeps giving me errors, stating the “variable doesn’t exist in this context”.
To further complicate matters, one of the Variables I’m trying to access is attached to a script, that gives it a different Int depending on what gameObject it is attached to. (It is a “Damage” int, attached to several bullets with different numbers, to remove health from my “Enemy” gameObject.
Okay, so here goes nothing, I’ll post my 3 scripts below - again, I am VERY sorry for re-asking this, but I just need some more specialized information, I honestly can’t wrap my head around what I’ve done wrong.
Enemy Script (Trying to access 2 other scripts)
using UnityEngine;
using System.Collections;
public class enemyHealth : MonoBehaviour {
private int health;
void Start () {
health = 1;
gameObject.GetComponent<score>();
}
void Update () {
if (health == 0)
{
death();
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("bullet"))
{
other.gameObject.GetComponent<bulletOne>();
health = health - damage;
}
if (other.gameObject.CompareTag("nuke"))
{
death();
}
}
void death()
{
numberScore = numberScore + 1;
Destroy(gameObject);
}
}
Score Script (Kind of place holder until I work out how to put it on GUI but it holds the “numberScore” var regardless)
using UnityEngine;
using System.Collections;
public class score : MonoBehaviour {
public int numberScore;
// Use this for initialization
void Start () {
numberScore = 0;
}
// Update is called once per frame
void Update () {
if (numberScore == 1)
{
Debug.Log("score counted up!");
}
}
}
bulletOne script - attached to 3 different bullets, with different variables for bulletSpeed and damage.
using UnityEngine;
using System.Collections;
public class bulletOne : MonoBehaviour {
public int damage;
public float bulletSpeed;
private Vector3 bulletMove;
void Start ()
{
}
void Update ()
{
bulletMove = new Vector3(0.0f, 1.0f, 0.0f);
}
void FixedUpdate ()
{
gameObject.transform.Translate((bulletSpeed * bulletMove) * Time.deltaTime);
}
}
Sorry if I’ve written this complicated! I just cannot wrap my head around it, anyone who can help me, Thank you SO much!