Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Spider_newgent · Aug 19, 2013 at 03:29 PM · triggerchange variable

change variable in script attached to parent using triggers

I'm trying to make a simple sliding door prefab which opens when the player touches a trigger object, and closes when the player stops touching the trigger.

I have a scaled cube as the door mesh, and attached a script, "autoDoorScript" The collision object is a non-renderable sphere with a script "autoDoorSensorScript" attached. The mesh is the parent of the sphere, and both are then attached to a prefab, "autoDoorPrefab"

It works using a Static variable to control variables between scripts. When the player touched the collision trigger, it set canOpen = true; in the mesh object. However, because of the Static variable, all doors opened simultaneously.

What I'm trying to achieve is this: When the player steps within the sensor object, the "onTriggerEnter" of the "autoDoorSensorScript" is called. This needs to "canOpen = true;" in the "autoDoorScript" attached to the door mesh in the same prefab. I can't use a static variable, as don't want all other objects effected.

I've searched the forums etc pretty extensively, but the methods employed are beyond my current skills. Are there any simple solutions?

It seems it'd be a pretty regular requirement to do something like:

 GetComponent(autoDoorScript).OpenAndClose();

If anyone wants to see my code, I'll post what I've got so far, but it's more a theoretical question. I know why I'm getting an error, just not how to fix it.

Thanks

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 Spider_newgent · Aug 19, 2013 at 03:44 PM 0
Share

my cube is literally just the cube primitive with some scaling to make it more door shaped. I needed a static initially to make the "canOpen" variable inside the "autoDoorScript" visible to the "onTriggerEnter" Event of the "autoDoorSensorScript"

That worked fine, like this:

 function OnTriggerEnter(other:Collider)
 { 
     if(other.gameObject.tag == "Player")
     {
         autoDoorScript.canOpen = true;
     }
 }

but then when I drag more than one door prefab into the scene, all of them open at the same time because the variable is static.

Thanks

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DaveA · Aug 19, 2013 at 03:32 PM

It would help to see your cube, but why do you think you need a static? Just take static off that variable.

I'm also unsure what you mean by 'parent' Are you actually creating separate GameObjects and parenting them? Or do you mean, a component (script) attached to the door?

Because what I would do is: 1. Create door from cube by scaling. 2. Add sphere collider to same door object 3. Add your script to same door game object

Then make a prefab of that. Each door is self-controlled.

Comment
Add comment · Show 4 · 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 Spider_newgent · Aug 19, 2013 at 03:57 PM 0
Share

Ah I understand!

I'll give that a go but can forsee an issue as you can only have one collider on each object.

I'll need a sphere collider for the trigger and a box collider to prevent the player walking through whilst the door is opening.

How to get around that?

avatar image Joyrider · Aug 19, 2013 at 03:59 PM 0
Share

@Spider_newgent , converted two answers to comments, be $$anonymous$$dfull of where you respond. Do not write your response as an answer unless it is one. If it is not, you can add comments to your questions or to the answers that are given to follow up.

avatar image Spider_newgent · Aug 19, 2013 at 05:12 PM 0
Share

Understood.

Didn't see the button there. Thanks for pointing it out.

I'll take better care next time.

avatar image Spider_newgent · Aug 19, 2013 at 06:29 PM 0
Share

Just in case anyone else has the same/similar problem, it is possible to have more than one collider on an object..

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Triggers not working - What's wrong with this script? 2 Answers

Initiate Animation 0 Answers

Change main Camera to another camera with a Trigger then back with a Timer. 1 Answer

How to get info from child triggers? 1 Answer

How to TAG the Player? 1 Answer


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