- Home /
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;
}
}
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;
}
}
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
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
Follow this Question
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