- Home /
Problem with Accessing Variables from Other Scripts in c#.
I have an enemy ship and a player which is sopposed to shoot a projectile at the enemy and then the enemy should pull a value from an array from a script attaced to a controller.
public GameObject gameController;
void OnTriggerEnter (Collider other) {
if (other.tag == "projectile1") {
health -= gameObject.GetComponent<Controller>().damage[0];
}
}
And I have a controller scrtipt.
using UnityEngine; using System.Collections;
public class Controller : MonoBehaviour {
public float[] damage;
void Update () {
}
}
I have tested this with hard coded values and it works just fine.
Just incase I somehow screwd up somewhere else in the script here is the full enemy script.(Some of the lines seem to be getting cut off in my preveiw so I there might be some brackets missing etc.)
using UnityEngine;
using System.Collections;
public class BasicAI : MonoBehaviour {
public GameObject gameController;
public GameObject target;
public float speed;
public float stopDistance;
public float attackDistance;
public float weaponCoolDown;
public Transform Barrel_L;
public Transform Barrel_R;
public GameObject projectile;
public float health;
public GameObject expolsionParticle;
float weapomTimer;
void Update () {
transform.LookAt (target.transform);
if (Vector3.Distance(target.transform.position, transform.position) > stopDistance) {
rigidbody.AddRelativeForce (0, 0, speed);
}
else {
rigidbody.AddRelativeForce (0, 0, -speed);
}
weapomTimer += 1 * Time.deltaTime;
if (Vector3.Distance(target.transform.position, transform.position) < attackDistance) {
Debug.Log("ready to fire");
if (weaponCoolDown < weapomTimer) {
Instantiate (projectile, Barrel_L.position, Barrel_L.rotation);
Instantiate (projectile, Barrel_R.position, Barrel_R.rotation);
weapomTimer = 0;
Debug.Log("fire");
}
}
if (health < 0) {
Instantiate (expolsionParticle, transform.position, transform.rotation);
Destroy (gameObject);
}
}
void OnTriggerEnter (Collider other) {
if (other.tag == "projectile1") {
health -= gameObject.GetComponent<Controller>().damage[0];
}
}
}
Sorry for that, as I said I tested the collision detection/damage system so that works, the problem seems to be this: health -= gameObject.GetComponent().damage[0]; particular line of code.
Answer by MakeCodeNow · Feb 22, 2015 at 04:55 PM
Arrays need to be allocated, not just declared. Somewhere you need to have damage = new float[X]; where X is the number of items you want to allow the array to store.
So now my code looks like this:
using UnityEngine;
using System.Collections;
public class Controller : $$anonymous$$onoBehaviour {
public float[] damage;
void Start () {
damage = new float[10];
}
void Update () {
}
}
But sill for whatever reason it dosen't work.
It looks like you're trying to access the 0'th element of an empty array. When you first instantiate (fancy word for using the "new" keyword) an array, all the elements are null - there's nothing there! Try putting something in the array at the 0th element and seeing if it still doesn't work:
damage = new float[10];
damage[0] = 30;
I'm not sure why you're using an array if you're only accessing the first element, but I'm sure you have your reasons.
Is damage
supposed to be public (it is, just wondering if that was on purpose)? The size is whatever you type into the Inspector. If you're just going to remake it in code, no reason to be public.
But, the point of a public is usually you want to enter Inspector values.
I think this is a design issue. What's the purpose of the damage
array at all?
The damage array is a chart of sorts like: projectile X dose X dmg.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Global array list c# 1 Answer