- Home /
Can't access a variable from another script through collider (C#)
Hello!
I'm having a problem while trying to access a variable that I have declared in another script. The problem is, that I'm need to check this variable from the object that I hit, BUT unity is telling me that this variable doesn't exist (I was thinking to look this variable through the collider, but it isn't working).
Here's my class that is attached on the object that will be hit:
public class CapsuleLoad : MonoBehaviour {
public int bulletsNo;
void Start () {
if (bulletsNo == 0)
bulletsNo = 1;
}
} And this is the code that I was thinking to use:
void OnTriggerEnter (Collider colliderObject){
if (colliderObject.GetComponent("CapsuleLoad")){
int currentBullets = colliderObject.GetComponent("CapsuleLoad").bulletsNo;
Destroy(colliderObject.gameObject);
}
The exact error that I'm getting is: Type 'UnityEngine.Component' does not contain a definition for 'bulletsNo' and no extension method 'bulletsNo' of type 'UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?);
(Yes, I'm using C#)
Answer by Seth-Bergman · Aug 11, 2012 at 07:04 PM
Just get rid of the quotes
if (colliderObject.gameObject.GetComponent(CapsuleLoad)){
int currentBullets = colliderObject.gameObject.GetComponent(CapsuleLoad).bulletsNo;
Destroy(colliderObject.gameObject);
}
If you use quotes, GetComponent will return a type of "Object", which then needs to be typecast. without the quotes, you get the actual type, in this case CapsuleLoad.
Now I'm receiving other problems:
Expression denotes a 'type', where a 'variable', 'value' or 'method group' was expected
The best overloaded method match for 'UnityEngine.Component.GetComponent(System.Type)' has some invalid arguments
Argument `#1' cannot convert 'object' expression to type 'System.Type'
(It indicates those errors in both lines where I've removed the quotes)
oops, change it to:
if (colliderObject.gameObject.GetComponent("CapsuleLoad")){
etc...
I'll edit it above
It does work to enter on the "if" script, but, on the other hand, the 'int' part don't work.
The error remains the same: Type 'UnityEngine.Component' does not contain a definition for 'bulletsNo' and no extension method 'bulletsNo' of type 'UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?);
you may need to split that up:
void OnTriggerEnter (Collider colliderObject){
CapsuleLoad cl = colliderObject.gameObject.GetComponent(CapsuleLoad);
if (cl){
int currentBullets = cl.bulletsNo;// note below
Destroy(colliderObject.gameObject);
}}
BUT, if you declare the int currentBullets inside that if statement, it only exists there, and you never used it..
I assume there is more to this.. are you trying to add bulletsNo number of bullets to an existing int?
then you would just say
totalBullets += cl.bulletsNo;
or something...
Would you believe if I told you that I just did that on my code hehe. But anyway, it worked!
I just need to explicitly tell the type of the instantiated object, and add the quote:
CapsuleLoad cp = colliderObject.gameObject.GetComponent("CapsuleLoad") as CapsuleLoad;
Thanks ;)
Your answer

Follow this Question
Related Questions
Access a C# script on collison 1 Answer
How to access children's colliders. 4 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Only use specific collider 2 Answers