- Home /
How I can replace a game object in a GameObject array ?,how to replace gameobjects of a array
Hi, I'm trying to create my first game(FPS), I want to build a simple inventory, so I created a GameObject array, and when I'm near to certain gameObject I should be able to pick up this gameObject and replace the old one in the array. The script runs correctly, but when I press "E" near the object nothing happens. Obviously "secondary = hit.transform.gameObject" isn't the right way to do this. Someone can help me, please? link text<-----script
,
using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine; public class Picker : $$anonymous$$onoBehaviour { private int units = 10; private int rifles_layer; private int pistols_layer; private int enviroment_layer; public Image cursor; public RaycastHit hit = new RaycastHit(); private bool pick; private bool rifle = false; private bool pistol = false; private GameObject holder; private Layer$$anonymous$$ask Layer; private GameObject primary; private GameObject secondary; void Start() { rifles_layer = Layer$$anonymous$$ask.NameToLayer("Rifles"); pistols_layer = Layer$$anonymous$$ask.NameToLayer("Pistols"); enviroment_layer = Layer$$anonymous$$ask.NameToLayer("Default"); } void Update() { holder = GameObject.Find("player/ST_avoider/firstPersonCamera/weaponHolder"); primary = holder.GetComponent().weapons[0].gameObject; secondary = holder.GetComponent().weapons[1].gameObject; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Vector3 fwd = transform.TransformDirection(Vector3.forward); if(Physics.Raycast(ray,out hit,units)){
if(hit.transform.gameObject.layer == rifles_layer){
cursor.GetComponent<Image>().color = Color.blue;
rifle = true;
}
else{
rifle = false;
}
if(hit.transform.gameObject.layer == pistols_layer){
cursor.GetComponent<Image>().color = Color.blue;
pistol = true;
}
else{
pistol = false;
}
if(hit.transform.gameObject.layer == enviroment_layer){
cursor.GetComponent<Image>().color = Color.white;
}
}
equip();
}
void equip(){
if(Input.GetKeyDown("e")){
pick = true;
}
else{
pick = false;
}
if(rifle == true && pick == true){
Debug.Log("rifle picked");
primary = hit.transform.gameObject;//I tried to replace the gameobject here
}
if(pistol == true && pick == true){
Debug.Log("pistol picked");
secondary = hit.transform.gameObject;//I tried to replace the gameobject here
}
}
}
Answer by highpockets · Aug 17, 2020 at 10:08 PM
You should really format your code, but I see your issue.
You assign a game object from an array to secondary, but secondary is now just a reference to the game object from the second position of your game object array. So now changing secondary, won’t do anything to the array, you have to change the array itself. If you like calling it secondary then you could set it up as a getter/setter for the second position of the game object array you want it to refer to by doing something like this:
[SerializeField]
private GameObject[] weapons;
public GameObject primary
{
get{ return weapons[0]; }
set{ weapons[0] = value; }
}
public GameObject secondary
{
get{ return weapons[1]; }
set{ weapons[1] = value; }
}
I made weapons an array you can set initially in the inspector and if you want to set a new value or get the Current value of primary at position 0 of the array than you just use primary and likewise for secondary. Hope that makes sense
No problem, you may want to accept it as the correct answer for future viewers