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 skyerz · Mar 31, 2013 at 04:25 PM · c#weaponkinect

Comparing two GameObjects to pick up items

Im trying to create a script for my 2d game, while hovering over the item, select it so that it then can be used, i found some code over the internet which relates closely to picking up a weapon but im having trouble following it how would you go about a weapon pick up

 if(selectedWeapon && Input.GetButtonDown("pickup")){
     for (var i : int=0 ;i<playerWeapons.weapons.length; i++){
         if(playerWeapons.weapons[i] == selectedWeapon.GetComponent(SelectableWeapon).weapon)
             return;
     }

Im doing my work in c# rather than java script and when i try to type this code out i get an error at where it would say getComponent().weapon(becuase it would be impossible for the compiler to know that selectedweapon has a variable of type weapon :S?), from what i understand, the code essentially asks whether the two gameobjects are the same. So in that case do i have to compare it in this way, can i not just refer to the tag of the object. Or is it more a case that selectedWeapon (in my case selectedTool) will have to be a custom class of some sort here is my code for. I'm literally a month in learning unity so this is pretty new, if there is any other way of doing this im open to any suggestions

 using UnityEngine;
 using System.Collections;
 
 public class SelectTool : MonoBehaviour {
     
     public Tools tools;
     public float startTime;
     public bool ScalpalSelected;
     public GameObject selectedTool;
     
     void Start()
     {
         tools = this.GetComponent<Tools>(); //in order to use this tools muyst be attached to the game object
         //this is essentially saying  with regards to this game object get the component named tools
     }
     void update()
     {
         /*this script will be attached to the cursor
          * essentially what could be done is to
          * 
          * 
          * 
          * 
          */ 
     }
     void OnCollisionStay(Collision Other)
     {
         startTime +=Time.deltaTime;
         
         if(startTime >5f){
             if(Other.collider.tag==("Scalpal"))
             {
                 selectedTool = Other.collider.gameObject;
                 Debug.Log(selectedTool+" What in gods good name is:");
                 //this essential creates a scalpal object out of selected tool
             }
             else {
                 selectedTool=null;
             }
             if(selectedTool){
                 for(int i=0;i<tools.utilities.Length;i++)
                 {
                     if(tools.utilities[i]==selectedTool.GetComponent(SelectableUtils).tool)
                         return;
                 }    
             }
     
             
                 ScalpalSelected=true;
                 renderer.material.color = Color.yellow;    
         }
     }
     void OncollisionStay(Collision other){
     
         startTime = 0f;
     }
     
 }
 


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 paulaceccon · Mar 31, 2013 at 08:27 PM

If I understood your question and your problem is here:

 if(selectedWeapon && Input.GetButtonDown("pickup")){
     for (var i : int=0 ;i<playerWeapons.weapons.length; i++){
         if(playerWeapons.weapons[i] == selectedWeapon.GetComponent(SelectableWeapon).weapon)
             return;
     }
 }

I guess you could do this:

 if(selectedWeapon && Input.GetButtonDown("pickup")){
     for (var i : int=0 ;i<playerWeapons.weapons.length; i++){
         if(selectedWeapon.GetComponent(SelectableWeapon).weapon && playerWeapons.weapons[i] == selectedWeapon.GetComponent(SelectableWeapon).weapon)
             return;
     }
 }
Comment
Add comment · Show 2 · 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 skyerz · Mar 31, 2013 at 09:06 PM 0
Share

where it has return, does that simply mean return true if conditions are met ? sorry to be confusing, essentially was looking for the c# equivalent to that line of code and just briefly what its actually doing.. in laymen terms

avatar image paulaceccon · Apr 01, 2013 at 12:01 AM 0
Share

No, you are not returning true. Doing this, if the conditions are met, you get out of the function, just this, doing nothing. It's like... Huum, if these conditions occurs, yout don't want to do nothing, so you just return...

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Swapping the Position of an element on certain conditions 0 Answers

An OS design issue: File types associated with their appropriate programs 1 Answer

Space Torpedo 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