• 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 svenyn · Apr 07, 2014 at 05:26 PM · checkactionflag

How to check for something to let an action happen?

Hi there,

I'm creating a video game right now. It's a 3d ball scroller, and I want it so that once I pick up something (a bar of soap in this case), my ball texture changes. This is already done, but I also want it to be that once my texture is changed, I'll be able to destroy another game object. So lets say I picked up the soap bar, and then I can destroy a virus simply by rolling over it, but ONLY if I have picked up the soap bar. Right now, it always destroys the virus, no matter nor I've picked up the soap bar or not.

My scripts are:

The soap bar:

 #pragma strict
 var coinEffect : Transform; 
 var coinValue = 1; 
 var Soap : Texture2D; 
 var TheBall : Transform;
 
 function OnTriggerEnter (info : Collider) 
 { 
     if (info.tag == "Player") 
     { 
         GameMaster.currentScore += coinValue; 
         var effect = Instantiate(coinEffect, transform.position, transform.rotation); 
         Destroy(effect.gameObject, 3); 
         Destroy(gameObject); 
         TheBall.renderer.material.mainTexture = Soap; 
     } 
 }

The virus:

 #pragma strict
 var coinValue = 1; 
 var TheBall : Transform;
 
 function OnTriggerEnter (info : Collider) { 
     if (info.tag == "Player") 
     { 
         GameMaster.currentScore += coinValue; 
         var effect = Instantiate(coinEffect, transform.position, transform.rotation); 
         Destroy(effect.gameObject, 3); 
         Destroy(gameObject); 
     } 
 }

So what do I have to add to make it so that I can only destroy the virus once I have picked up the soap?

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 perchik · Apr 07, 2014 at 05:35 PM 0
Share

I formatted the code for you this time; next time use the 101010 button to format it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by perchik · Apr 07, 2014 at 05:40 PM

I'd probably restructure your logic so that you have one script for the ball/player.

The logic would be something like:

 if I collided with something:
     was it a soap bar?:
         if yes, change texture to soap 
     was it a virus?
         if so, am I already soap? 
              if yes, kill the virus
              if no, too bad, can't to anything


That way, when you want to add more items, you won't have to add new scripts every time (although you could have more scripts that do specific things when you collide with those type of objects)

That script would live on the player, then you could either look at the collider's name or tag to decide what thing you hit. You could also create a variable of type boolean to save whether you hit the soap or not:

     var isSoapy : boolean;
     
     
     function Start()
     {
        isSoapy = false;
     }
     
     function OnTriggerEnter (info : Collider) 
     { 
         if (info.tag == "Soap") 
         {
              isSoapy = true;
         }
     
         if (info.tag == "Virus")
         {
             if(isSoapy ==true) //could also write if(isSoapy)
             {
                //kill the virus
             }
         }
 
    }
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 svenyn · Apr 07, 2014 at 06:10 PM 0
Share

Thanks a lot!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Add a flag to be checked on an object. 1 Answer

How can I know if a gameObject is 'seen' by a particular camera? 11 Answers

How to check iphone network connection 0 Answers

Check if an array contains certain number combos? 1 Answer

Reference boolean check from other script (not working) 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