- Home /
How to access a trigger objects variables:
Hi there, I'm sorry if this has been covered before, but I have searched hi and low for the solution and the forums and otherwise, and I'm very nearly there... I think...
I would like to know how to access a variable from another object (that has triggered with current object) scripts in order to eventually change it. The script on both objects is called "DamageHandler" and the variable is bool collisionTrigger. I am currently just trying to show it in the debug log before I move on to trying to change it. Here's the relevant code for this part...
static bool collisionTrigger = false;
void OnTriggerEnter2D(Collider2D other)
{
// Store the other objects collisionTrigger state
bool otherColTrig = other.gameObject.GetComponent<DamageHandler>().collisionTrigger;
Debug.Log (otherColTrig);
}
I've also tried jumping straight into:
Debug.Log (other.gameObject.GetComponent<DamageHandler>().collisionTrigger);
Both recieve this error:
Assets/Scripts/DamageHandler.cs(97,100): error CS0176: Static member `DamageHandler.collisionTrigger' cannot be accessed with an instance reference, qualify it with a type name instead
Not sure where to go from here. Any help would be amazing!
Cheers in advance,
Pete
O$$anonymous$$, Ok cool, I'll remove static then. I added static after reading another similar forum post, but I guess there situation was still a little different to $$anonymous$$e.
So let me get this straight... Static makes a variable belong to no particular object, so you would only make a variable static if there was only 1 instance of its script? Is that close? I best read up on static...
So my my script "Damage Handler" with the variable "collisionTrigger" is a component of every object currently in existence in my game. And it is within the same script that I am accessing the other objects collisionTrigger.
Well I'll try these ideas out and research static variables when I get on it later and report back. Cheers for your help guys!
Pete
Answer by IsaiahKelly · May 16, 2015 at 07:02 AM
This error is because your collisionTrigger variable is set to static. Static variables can not belong to any GameObject. Therefore, you can't grab a reference to it from another GameObject because it doesn't have one! Remove the Static keyword from collisionTrigger and the error should go away.
Answer by smallbit · May 16, 2015 at 07:02 AM
I am not sure if your collision trigger should be static, if you have multiple instances of the script maybe you want to have this variable per object so only objects that collide with something will have it as true.
Now apparently the best way to access static variable (while keeping it private) you "wrap" variable around it for instance:
private static bool collisionTrigger= false;
public static bool CollisionTrigger
{
get { return collisionTrigger; }
set { collisionTrigger= value; }
}
and than in other script you just call:
bool otherColTrig = other.gameObject.GetComponent<DamageHandler>().CollisionTrigger;
Answer by iron_attorney · May 17, 2015 at 04:41 PM
Yep! It was because of the static part. It worked perfectly as soon as I removed that. I've now also looked up static variables and I'm pretty sure I understand what that means now.
Cheers to both of you for your help!
Your answer
Follow this Question
Related Questions
Accessing and using another objects script variable 1 Answer
Checking 2DCollisions During a single Update 0 Answers
Detector NPC on trigger zone. 0 Answers