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 Trucido · May 30, 2013 at 08:23 PM · triggervaluearea

Variable doesn't change when box hit trigger area

I'm trying to make a trigger area change value after a specific object (cube) enters the area (cube trigger), but when I move the cube to the area, the "debug" value doesn't change (from 1 to 2). What am I doing wrong?

var other : GameObject;

//var fuel_points : int = 100;

var debug : int = 1;

function OnCollisionEnter (other : Collision) {

   if(gameObject.tag=="other"){

   debug ++;

   //Destroy(other.gameObject);

     //fuel_points = other.GetComponent(fuel_points);

               //fuel_points += 10;



//}

//else(game do blablablabla)

  //     Destroy(other.gameObject);    

 }

 }

 
Comment
Add comment · Show 2
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 Memige · May 30, 2013 at 08:29 PM 0
Share

Ok, to avoid confusion if I can, I'm going to refer to the object as Cube and the area as Area. You have your script attached to Area and you are wanting your code to trigger when Cube enters Area. Your code is looking for an object that is tagged as "other", can you confirm that Cube has an "other" tag?

avatar image Memige · May 31, 2013 at 04:08 PM 0
Share

Hey Trucido, Just following up on this, any progress? Did any of the answers help you?

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · May 30, 2013 at 08:35 PM

gameObject.tag is the tag of the object to which this script is attached, and you should use OnTriggerEnter instead:

 function OnTriggerEnter (other : Collider) {
   // verify whether the tag of the object entering the trigger is "other":
   if (other.tag=="other"){
     debug ++; // if so, increment debug
   }
 }

NOTE: This script is comparing the entering object's tag to "other" - is this what you actually want to do? Usually the player is tagged "Player", but of course you can use any tag you want - provided that you have registered this tag previously.

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

Answer by Trucido · May 30, 2013 at 09:04 PM

Alright, thank you for your answers but this is how it works:

  1. Picking up a cube with a certain script (the one I posted) and releasing it into the trigger area.

  2. The cube is a catalyst that will increase the int variable

Problem is, when it collides it doesn't change, yes I've selected the correct "other" and the pickup is same target, but is there a chance that they are colliding with each other? It doesn't show up any errors while doing this.

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 Memige · May 30, 2013 at 09:13 PM 0
Share

Please post responses as comments on the associated answer, or as a comment on your question if it is a general statement. Answers are for answering.

Back to your problem though: gameObject.tag == "other" is not going to work because it will either always be true or always be false unless you are dynamically changing tags. $$anonymous$$y recommendation would be to A. Use a more informative tag, "other" is very confusing in this context. and B. check the tag of the object that you collided with, rather than your own tag. Aldonaletto's example is best practice for this.

avatar image
0

Answer by Memige · May 30, 2013 at 08:35 PM

Edit 2**

Based on your response I'm gathering that what you are doing is that your collision script is attached to the area, and it is listening for collision of these cubes that will give it fuel. toward that end I would propose the following restructure: Create a new Tag called "Fuel" assign the cubes this new tag, then swap out your current code for the following:

 var debug : int = 1;
 
 function OnTriggerEnter(other : Collider)
 {
     if(other.tag == "Fuel")
     {
         debug++;
         //DoMyAwesomeLogic();
     }
 }

Edit** Aldonaletto's response is a little more concise and technically more correct, I'd recommend his approach.

Original**

Okay, actually I just took a second look and here are the two things I'm seeing. As I mentioned in my comment, you are looking for an "other" tag, please make sure you are not confusing this with your other: Collision variable. Secondly, you are not checking the collided object but rather the object the script is on, try changing

 if(gameObject.tag=="other")

to

 if(other.gameObject.tag=="other")

and as mentioned, make sure that you have an "other" tag on the cube

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

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

15 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

Related Questions

Multiple Cars not working 1 Answer

How to trigger a Script from an Object. 0 Answers

How to make the trigger work only once. (SOUND) 1 Answer

Make an area that is only accessible through buying a game addon? 1 Answer

I want my trigger sound only to play once! 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