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 Mctickle14 · Apr 01, 2012 at 05:15 PM · javascriptlanguagepick up

Help needed with a pickup script

ok i am trying to make a script where you pick up materials which you use later in the game to make other things but whenever the main character picks up an item on the collision list it adds to all of the materials instead of the one assigned... i feel like its really obvious what i did wrong but i cant figure out what

var addmat1 = 5;

var mat1 = 0;

var addmat2 = 1;

var mat2 = 0;

var addmat3 = 2;

var mat3 = 0;

var addmat4 = 2;

var mat4 = 0;

var collide1 : GameObject;

var collide2 : GameObject;

var collide3 : GameObject;

var collide4 : GameObject;

function OnTriggerEnter (Collision: Collider) {

if (Collider.GameObject == collide1);

mat1 += addmat1;

if (Collider.GameObject == collide2);

mat2 += addmat2;

if (Collider.GameObject == collide3);

mat3 += addmat3;

if (Collider.GameObject == collide4);

mat4 += addmat4;

}

function OnGUI () {

GUI.Label (Rect (10, 10, 100, 20), "Nails:" +mat1);

GUI.Label (Rect (60, 10, 100, 20), "Wood:" +mat2);

GUI.Label (Rect (110, 10, 100, 20), "Metal:" +mat3);

GUI.Label (Rect (160, 10, 100, 20), "Misc:" +mat4);

}

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
0
Best Answer

Answer by save · Apr 01, 2012 at 10:53 PM

You need to set the type of your variables, it isn't crucial to the problem but important for the future.

 var addmat1 : int = 5;

You're not using the variable returned from the collision.

 function OnTriggerEnter (Collision: Collider)
 //Should be
 function OnTriggerEnter (other : Collider)
 //As this is the data from the other colliding object, not the collision
 //Then use
 if (other.GameObject == collide1) {
     //As garner stated do not end the if-statements with semicolon
 }

You might as well use the colliders as variables instead of calling the GameObjects if you don't plan on using the reference later.

 var collide1 : Collider;
 if (other == collide1) {}

I recommend you to start using #pragma strict in the top of your scripting documents to disable dynamic scripting, you will have a much easier pipeline for errors, and the main reason, much faster code.

Although when creating pickups it's usually better to let the pickups have values that passes on to a handler. So if you'd create a pickup-script and attached it to the GameObject, then you'll be able to give it different amounts of values, for instance a pile of gold is more worth than a single gold coin but increases the same value.

You could start off with an enum setting the type of pickup, then give that particular pickup a value.

 enum PICKUP {
     Nails,
     Wood,
     Metal,
     Misc
 }

 //From the inspector you can switch this and make the object into a prefab
 var pickupType : PICKUP = PICKUP.Nails;
 var amount : int = 1;

Now when the collision occurs you can ask the colliding object what it is and how much it is worth.

 function OnTriggerEnter (other : Collider) {
     if (!other.CompareTag("Pickup")) return; //Make sure that this is a pickup else exit (needs a tag set in the Inspector)
     var pickupScript : PickupScript = other.GetComponent(PickupScript); //Get the script from the colliding object
     DoSomething(pickupScript.pickupType, pickupScript.amount); //Send the type and amount into a function
 }

 //Take care of the amount that should get stored into corresponding variable
 function DoSomething (type : PICKUP, amount : int) {
     switch (type) {
         case PICKUP.Nails: mat1+=amount; break;
         case PICKUP.Wood: mat2+=amount; break;
         case PICKUP.Metal: mat3+=amount; break;
         case PICKUP.Misc: mat4+=amount; break;
     }
 }
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
0

Answer by garner · Apr 01, 2012 at 10:00 PM

you've ended the if statements with semicolons they shouldn't have semicolons this is why it's assigning everything regardless. Also try Collider.GameObject.name and using 'else if' is useful here.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Sorting through the same variable attached to several GameObjects from least to greatest, then returning an enum state based on that order 0 Answers

Errors with script using Javascript. 2 Answers

[SOLVED] Enemy Script : Mob doesn't take damage 2 Answers

JavaScript and Ajax second addition Tutorial 11 case problem 2 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