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 soulbound · Oct 09, 2014 at 10:52 AM · collect

Collection quest trouble. JavaScript

Hey guys and girls

Just asking a question in regards to a sort of quest system in JavaScript I Currently have two objects Sphere 1 and Sphere 2 these two spheres both have colliders around them as shown below, I want the player to walk through both colliders then set both the spheres to SetActive = false easy enough, but then I want it too check when the player walks into a collider if the other object is false, so if I walk into sphere 1 then check to see if sphere 2 is SetActive = false if the sphere reads as false, turn on another collider(not shown in the image) alt text

this is what I am currently using, two scripts, one on each collider around Sphere1 and Sphere2

 var collider1 : GameObject;
 var Sphere1 : GameObject;
 
 function OnTriggerEnter () 
 {
 Sphere1.SetActive (false);
 
     if(GameObject.FindWithTag("Sphere2").SetActive == false)
     {
         collider1.SetActive (true);
         }
 }
 

 var collider1 : GameObject;
 var Sphere2 : GameObject;
 
 function OnTriggerEnter () 
 {
 Sphere2.SetActive (false);
 
     if(GameObject.FindWithTag("Sphere1").SetActive == false)
     {
         collider1.SetActive (true);
         }
 }
 

not sure where I am going wrong but any help would be greatly appreciated

thanks!

unityanswers.jpg (65.9 kB)
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 HarshadK · Oct 09, 2014 at 11:18 AM

You need to check if the game object is active or not using GameObject.activeInHierarchy.

So your if condition becomes:

 if(GameObject.FindWithTag("Sphere2").activeInHierarchy == false)

and same for another game object.

But since if the game object is inactive then GameObject.FindWithTag will not be able to find it anyways. You need to cache a reference to the required game object beforehand.

For this your script becomes:

Script on Collider 1:

 var collider1 : GameObject;
 var Sphere1 : GameObject;
 var Sphere2 : GameObject;
 
 function Start()
 {
     // You can use this or you can assign it directly in inspector. You should assign it in inspector if this game object is going to be inactive at start.
     Sphere2 = GameObject.FindWithTag("Sphere");
 }
 
 function OnTriggerEnter () 
 {
 Sphere1.SetActive (false);
  
     if(Sphere2.activeInHierarchy == false)
     {
         collider1.SetActive (true);
         }
 }

Similarly you need to do for second colllider.

Comment
Add comment · Show 3 · 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 soulbound · Oct 10, 2014 at 04:45 AM 0
Share

awesome thanks for your reply I tried this out today an for some reason It runs the start function but when I enter the second trigger It doesn't get past the If statement still,

 var collider1 : GameObject;
 var collider2 : GameObject;
 var Heart : GameObject;
 var Feather : GameObject;
  
 function Start()
 {
     // You can use this or you can assign it directly in inspector. 
     Heart = GameObject.FindWithTag("Heart");
     Debug.Log ("StartFuncWorking");
 }
  
 function OnTriggerEnter () 
 {
 Feather.SetActive (false);
  
     if(Heart.SetActive == false)
     {
         collider1.SetActive (true);
         collider2.SetActive (true);
         Debug.Log ("heart and feather found");
         }
 }


this is my updated script, It has changed a bit to suit the purpose in the game. any ideas? I walk into one collider, then the second one and the objects itself gets turned off but still nothing after that happens

avatar image HarshadK · Oct 10, 2014 at 05:26 AM 0
Share

You should check if object is active using GameObject.activeInHierarchy.

So your if statement becomes:

 if(Heart.activeInHierarchy == false) {
     collider1.SetActive (true);
     collider2.SetActive (true);
     Debug.Log ("heart and feather found");
 }

avatar image soulbound · Oct 10, 2014 at 05:34 AM 0
Share

ahh okay I put my if statement as

if(GameObject.FindWithTag("Sphere2").activeInHierarchy == false)

and it wasn't working that makes more sense

thanks again!

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

2 People are following this question.

avatar image avatar image

Related Questions

Resouce script help 3 Answers

Android Game crashing when collecting collectibles 0 Answers

How do you create a "collect" objection that corresponds with the enemy behavior in C#? 1 Answer

GC collects variable that is still in use 1 Answer

Time release Collectable 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