Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by iron_attorney · May 16, 2015 at 03:50 AM · ontriggerenter2dother object variables

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iron_attorney · May 16, 2015 at 04:32 PM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

My trigger for my parent object gets triggered from child and parent,My parent object gets triggered from the child object 2 Answers

"Private member OnTriggerEnter2D is unused"? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges