• 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 Myhijim · Jul 21, 2012 at 12:41 AM · vector3distancepickups

Multiple Objects Interfereing with each other?

Hi guys, well i had an idea for weapon pickups in my game that involved only on prefab for all of them called "WeaponPickup".

This object has a script called "scrPickup" and from the inspector every variable is set, eg. what model to be displayed and what ammo to be recieved if it is picked up. Within this script the Vector3.Distance is measured from the player to the pickup and tested if it is less than 3.

Now this works 100% if there is only one of these "WeaponPickup" but with more than one it will not show the text that needs to be displayed.

Well so far this is my code in "scrPickup" and i was wondering how to make it compatible with more than one of the same instance with different values.

This script is run in the update function

     function PickupCheck()
 {
 var playerScript =  player.GetComponentInChildren(scrPlayer);
 
     playerDistance = Vector3.Distance(transform.position,player.localPosition);
     pickupText = "Press 'F' to pick up " + pickupName;
  if (playerDistance <= 3)
  { 
  playerScript.pickupText = pickupText;
  
  if (Input.GetKeyDown('f'))
  {
  playerScript.weapon[playerScript.currWeaponSlot] = pickupName;
  print('You picked up the' + pickupName);
  playerScript.pickupText = '';
  Destroy(gameObject);
  }
  } 
  else
  {
  playerScript.pickupText = '';
    }
 }

Now this script works on one of the 'WeaponPickups' in the scene but no others

Thanks for any help ~ Myhijim

Comment
Add comment · Show 7
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 Myhijim · Jul 21, 2012 at 12:58 AM 0
Share

Would it involve for instance, a loop?

avatar image whydoidoit · Jul 21, 2012 at 08:48 AM 0
Share

It's slightly odd that you are checking a distance between a localPosition and a global position. Do you get any errors in the log?

avatar image Seth-Bergman · Jul 21, 2012 at 08:50 AM 0
Share

I saw that too, but as long as the Player is not parented to anything else, it's simply parented to the world anyway LOL

avatar image Seth-Bergman · Jul 21, 2012 at 08:56 AM 0
Share

After the first pickup, select one of the other pick-ups in the hierarchy panel.. then pay attention to the variables (playerDistance in particular), see if they're working..

avatar image Myhijim · Jul 21, 2012 at 12:55 PM 0
Share

The reason for the local position was because I was originally referring to the first person character controller

Show more comments

2 Replies

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

Answer by Myhijim · Jul 22, 2012 at 12:50 AM

I ended up fixing it by moving the entire code to the OnGUI function, this had to do with how i was calling it in the On GUI function, I was calling in On GUI

 if (playerDist <=3)
 {}

Thus this code was basically crossing itself out because one would test positive to the distance and the other would'nt.

Thanks for all the assistance anyway!

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 whydoidoit · Jul 21, 2012 at 08:50 AM

If you have more than one object then the fact you always reset pickupText means that all of the items are setting it to empty, not just the one that you were close to.

I suggest you add a GameObject reference to playerScript and set it to the pickup when it is the one which is close, then only ever reset the pickupText if the game objects match.

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 Myhijim · Jul 21, 2012 at 12:52 PM 0
Share

Thanks for the reply, I have tried this before, however I am ai$$anonymous$$g to have a single pickup prefab which each one through the inspector can have their value changed (ie var name), and this the pickupText changes accordingly

For example if one "WeaponPickup" name = gun, the print text will display ".... To pickup gun" and then if I make another "WeaponPickup", same prefab, just change the var name in the inspector to let's say beans it will say "....To pickup Beans" if it is within the correct distance

avatar image whydoidoit · Jul 21, 2012 at 01:09 PM 1
Share

$$anonymous$$y point is that your code currently sets

    playerScript.pickupText = '';

When the pickup is not in range - this will mean that all the pickups not in range set the text to ''

I am not suggesting that you do anything in the inspector - I am suggesting you set that variable in the code you have posted.

   if(playerDistance < 3) {
         playerScript.currentPickup = this;
         playerScript.pickupText = pickupText;
         ...
   }
   else if( playerScript.currentPickup == this)
   {
         playerScript.pickupText = "";
   }
avatar image Myhijim · Jul 21, 2012 at 01:26 PM 0
Share

Ah the this function! Thank you do much I will test this soon

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distance Variable Won't Change. What's Wrong With My Script? 1 Answer

Best Way To Find Distance 2 Answers

Distance Changing Conversions (1000m = 1km 1200m = 1.2km etc) 3 Answers

Check distance between multiple objects 1 Answer

C# geting player position (vector3) 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