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 Gamevara · May 26, 2016 at 08:43 PM · inspectorvalueplaymode

How to make inspector value effect on play mode?

Dear gurus and hobbyist,

I have a strange feeling that I'm missing something very simple. I have a ball game and during the game I got some "candies" (prefabs) which make the ball move slower or faster. The value of the inspector (int) change when I collect those candies, but that doesn't effect the actual game when I'm in play mode. It works perfectly fine if I change the the value before hitting the play button. So how can I say the game that it should use the updated value?

 public class BallFast : MonoBehaviour {
     
     private Ball ballSpeed;
     public float timeSpeed = 0;
 
     void Awake() 
     {
         ballSpeed = GameObject.FindWithTag("Ball").GetComponent<Ball>();
     }
     void Update () 
     {
         if (timeSpeed > 0)
         {
             timeSpeed -= Time.deltaTime;    
             //Debug.Log ("Update time: " + (int)timeSpeed);
         }
            else //if (timeSpeed < 0)
         {
             ballSpeed.ballInitialVelocity = 600;
         }
     }
      void OnTriggerEnter (Collider col) 
     {
         if (col.gameObject.tag == "Player") 
         {
              timeSpeed += 10f;
             ballSpeed.ballInitialVelocity = 800;
         }
     }
 }
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

1 Reply

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

Answer by instruct9r · May 26, 2016 at 09:03 PM

What happens with the Candies, when you collect them? Do they continue to exist in the scene? Keep in mind, that if you are destroying or de-activating them, their update will stop working.

You know, it's a good practice to keep the player's (In your case "Ball") behavior on the actual player and not on the collectibles.

So maybe you shoud move the actual code from the Update, to the Player's Update. And then just make the candies to send the new Velocity and the time, that this velocity shoud be active...

In your code, at start all of the candies should find the player which is slow (Especially with GameObject.Find ).

  • You don't need to find the Player in the Awake. When the collision happens, you can just get the component of the Player, from the collider.

  • In the scenario below, you can just tell the player to update the velocity and let it do the rest of the stuff. This way you can destroy the candy immediately after that.

// On The Candy

 private Ball ballSpeed
 
 void OnTriggerEnter (Collider col) 
      {
          if (col.gameObject.tag == "Player") 
          {
              col.gameObject.SendMessage("UpdateVelocity", 800, 10);
          }
      }
 

 // On The Player Script

 public float defaultVelocity = 600f;
 
 private float timeSpeed = 0;
 
 void Update()
 {
       if (timeSpeed > 0f)
      {
            timeSpeed -= Time.deltaTime;
      }
      else
      {
           ballInitialVelocity = defaultVelocity;
      }
 }
 
 // Update the velocity and time, that it shoud be active for...
 public void UpdateVelocity(float newVelocity, float timeActive)
 {
      timeSpeed = timeActive;
      ballInitialVelocity = newVelocity;
 }

P.S. Code might have errors. I've wrote it here... But i hope you got the idea...

Comment
Add comment · Show 5 · 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 Gamevara · May 27, 2016 at 09:10 AM 0
Share

Dear instruct9r,

thank you for your reply. The candies indeed exist quite a long after I collect them. I will try to change the behavior so the ball have necessary scripts. I'm not familiar with gameObject.Send$$anonymous$$essage but I'll find out and give a try.

I post my solutions here if any. Thanks.

avatar image instruct9r Gamevara · May 27, 2016 at 10:08 AM 0
Share

Cool. If you have some issues post them. I'll try to help.

One Note: Don't post comments as Answers! Use the comment button. Answers are only, when you post, answer to some question..

avatar image Gamevara · May 28, 2016 at 08:46 PM 0
Share

Stupid me, I never use the new velocity. It was only use one when starting. Arrgh!!

avatar image instruct9r Gamevara · May 28, 2016 at 08:58 PM 0
Share

Well. You set it in the OnTriggerEnter, but i coudn't see what's happening in the Ball script.

ANyway, glad you managed to find the issue :)...

avatar image Gamevara instruct9r · May 28, 2016 at 10:24 PM 0
Share

Yes, it's good the problem is so obvious. However I don't have any clue how to increase force for the moving object, so it still will continue the same vector. $$anonymous$$aybe another question should be ask after some weeks.

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

45 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to preserve array of custom objects in inspector? 0 Answers

Is the inspector Value accessable? 1 Answer

Public variable in script doesn't update in Unity inspector when saved 1 Answer

Saving values generated in game to use in prefabs. 2 Answers

Unity crashes when I change values on inspector 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