Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 zachypin · Jun 30, 2011 at 06:06 PM · updatefunctioninheritanceoop

Update Function Work Around Question

So I have a parent object called "Animate" and it has two children. "AnimateCircle" and "DownButton".
Now for the explanation of my dilemma:

Animate.js: This class looks at DownButton.cs and checks to see if a variable is true.

 public static var downbutton : DownButton;
 
 function Start()
 {
     //find children and look at appropriate classes
     downbutton = GetComponentInChildren(DownButton);
 }
 function Update () 
 {
     playanimation = false;
     
     if(downbutton.WasDownPressed())
     {
         playanimation = true;
     }
 }
 function PlayAnim()
 {
     return playanimation;
 }

DownButton.cs: This class returns a boolean value which is only true when an object is clicked on.

 public class DownButton : MonoBehaviour 
 {
     public static bool downPressed = false;
     void Update ()
     {
       //NOTE HERE
     }
 
     void OnMouseDown()
     {
         downPressed = true;
     }
     
     void OnMouseUp()
     {
         downPressed = false;
     }
     public bool WasDownPressed()
     {
          return downPressed;
     }
 }

AnimateCircle.js: This class looks to the parent "Animate" to see if a value is true so it can play an animation.

 public static var animate : Animate;
 
 function Start()
 {
     animate = transform.parent.GetComponent(Animate); 
 }
 
 function Update () 
 {
     if(animate.PlayAnim())
     {
         animation.Play("animationone");
     }
 }

Okay, so what you currently see works, but it needs to be altered. Currently if I hold the button down on the object the object will continue to animate, this is because in DownButton.cs the OnMouseDown() function is where the boolean is being set to true. What I've done before is make the boolean true at OnMouseUp() and then set the boolean to false in the Update function, where I wrote "//NOTE HERE". However this does not work in this case because I have a message being sent from child to parent to child. By the time the message is delivered the update function has ran again and has set the boolean back to false. I was wondering if someone had a way to make this work, or a different method I can think of that might work would be to pass the function value straight from child to child, though I don't know the syntax for that. As you can see in Animate.js I'm using GetComponentInChildren to look at the child value in DownButton.cs and then Looking up at the parent from AnimateCircle.js to Animate.js using transform.parent.GetComponent(Animate).

Comment
Add comment
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

2 Replies

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

Answer by flaviusxvii · Jun 30, 2011 at 07:26 PM

Like this maybe?

 public class DownButton : MonoBehaviour 
 {
     public static bool downPressed = false;
 
     void OnMouseUp() // or OnMouseDown.. whatevs
     {
         downPressed = true;
     }
 
     public bool WasDownPressed()
     {
          if(downPressed) {
              downPressed = false;
              return true;
          }
          return false;
     }
 }

125

Comment
Add comment · Show 1 · 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 zachypin · Jun 30, 2011 at 08:12 PM 0
Share

oh you made life easier. thank you

avatar image
0

Answer by zachypin · Jun 30, 2011 at 07:20 PM

Surprised 11 views and no answers. Pretty easy outcome. Just need to use BroadcastMessage() or SendMessage() but need to make sure to reference the "sibling".

 var sibling : GameObject;
 
 function Start()
 {
     sibling.BroadcastMessage("MakeCallToThisClass");
 }
Comment
Add comment · Show 1 · 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 flaviusxvii · Jun 30, 2011 at 07:27 PM 0
Share

Hey.. I was answering you. It just took several reads to figure out what you were talking about.

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

Can you redefine functions with names like Update(), etc.? 2 Answers

An OS design issue: File types associated with their appropriate programs 1 Answer

Does Unity support multiple Inheritance? 3 Answers

I have objects that have the same set of variables, but different desired behaviors. How can I do this without repeating variable declarations across all behaviors? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 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