Accessing another scripts Variable
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!
gameObject.GetComponent<score>();
is just a statement that doesnt do anything for you. You retrieve the Score component from the gameobject that script is attached to but you dont actually do anything with it.
Look again at the other GetComponent information.
Answer by Brocccoli · Mar 21, 2016 at 08:07 PM
Expanding on @meat5000's comment
You are calling
gameObject.GetComponent<score>();
and
other.gameObject.GetComponent<bulletOne>();
but you are never assigning the return values to a variable in the scope of the script. You never declare your variable "numberScore" anywhere in your enemyHealth script and you never declare "damage" either.
You need to declare these variables and assign to them just like you do "health" at the top of your enemyHealth script.
GetComponent() just returns the Component of type T attached to that game object if it is present. If you never assign it a value in the local script, than you are essentially getting the MonoBehavior and never giving it a value, so it essentially gets thrown away.
I suggest reviewing some of the Unity scripting tutorials.
Watched the video - thanks.
So, I'm going to sound extremely stupid, but.....
If I'm re-declaring all the variables, what's the difference between pulling them from another script or not? could I not just do the same thing, without GetComponent? or is GetComponent to make it change in the other script?
Also, is there then no way, to have the value be different depending on the bullet that hits me, without writing new variables for absolutely everything, and make all the bullets separate tags?
I was hoping
"damage" would equal 1, when hit by the basic bullet ( which is set up in public variable / the inspector) "damage" would equal 5, when hit by the "shotgun" bullet (again deter$$anonymous$$ed in the inspector) and "damage would equal 10, being hit by the "missile" bullet (again, inspector)
would declaring "Damage = 'x'" not then just override the 1/5/10 numbers from the other script - for example -
"Enemy has 15 health" "Enemy is hit by missile bullet" "Enemy has 5 health" - but if I was declare "damage = 2" would it not be "enemy is hit by missile bullet" "enemy has 13 health"
I am so sorry if I'm not making sense, I am just really struggling with this concept. If I have to re-declare everything, I just don't understand the point of accessing it from another script in the first place?
Each time you add a script component to a GameObject, it is its own instance of that script component. So if you have 3 bullets and attach the bullet script to all 3 of them, you can individually assign the damage of each one.
Calling GetComponent() just gets you a reference to the instance of that script and whatever variables you have declared. Once you have the reference, you need to access the variables via script reference variable.public variable that you want in the script.
These are all basic concepts of OO design, and I would suggest reading a bit further through those tutorials before continuing.
Good work so far though, keep it up!!
@Fingerlights please upvote the answer if it helped you. Thanks.
Okay, I think I understand what I was getting wrong now... maybe.
it should be public int scores;
"scores = gameObject.GetComponent Locate correct variable in score ();
Right? :D (Sorry!)
Your answer
![](https://koobas.hobune.stream/wayback/20220612072753im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Accessing script from another object 1 Answer
Unity {[(2D)]} RotateAround while jumping 0 Answers
Why do I only have left/right mouse movement 1 Answer
How do i make my enemies face the player? 2D 0 Answers
CAMERAS ERROR , I NEED HELP 0 Answers