- Home /
Add GameObject to GameObjectArray[]
Hi, I want to write an inventory in JS.
How do I add an existing object in my scene to a GameObjectArray[] inside another sceneobject by clicking on it?
Thank you guys! $$anonymous$$y approach was to split the script, one on an inventory-object, which has the gameObjectArray. And the second one lying on the object itself, which I want to put into the Array. But I don't know how to access the first script from the second script and put the object into the Array. Is there a way?
This is what I wrote on the object itself, but it doesn't work:
 function On$$anonymous$$ouseOver(){
 
  if (Input.GetButtonUp("Fire1")){
  
  var go = GameObject.Find("Inventory");
  var goc = GetComponent(InventoryTest);
  goc.Add(this.gameObject);
  
  }
 
 }
var goc = go.GetComponent(InventoryTest);
but if the inventory is always the same object, you could move the first two lines to the Start function.
Yes it is and I tried it, but always I get the same error: 'Add' is not a member of 'InventoryTest'.
Answer by Eric5h5 · Jul 21, 2012 at 05:15 PM
Use List, rather than built-in arrays, which aren't generally meant to have objects added or removed.
They are, yes, but how much faster depends on the type...for GameObject, there's little difference.
Answer by Seth-Bergman · Jul 21, 2012 at 04:25 PM
 var inventory GameObject[] = new GameObject[20];
 var count = 0;
 
 function Update()
 {
 if(Input.GetMouseButtonDown(0))
 {
 
       CheckInventory();
    }
 
 }
 
 function CheckInventory(){
 
   var ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
 
   var hit : RaycastHit;
 
        
 
         if (Physics.Raycast (ray, hit))
         {
         for(var i = 0;i < inventory.length;i++)
         {
          if(hit.collider.gameObject == inventory[i]) //cycle through inventory
          return;        //if Game Object is already added, don't add it
         }
 
        inventory[count] = hit.collider.gameObject); //else add the object
        count++;
      }
 
 }
(untested)
Answer by Artifactx · Jul 21, 2012 at 04:27 PM
I would do something like this, I have written it in C# since thats the language I know. But I'm sure you can translate it to JS.
It throws a raycast on the mouse position and puts the hit object into the array.
 public GameObject[] GoArray = new GameObject[10];
 int index = 0;
  
  // Update is called once per frame
  void Update () 
     {   
 
     /// <summary>
     /// Throw a raycast from the mouse position
     /// and adds the hit object to the array
     /// </summary>
      if (Input.GetMouseButtonDown(0))
         {
         Ray ClickRay = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
         RaycastHit ClickRayHit;
         if (Physics.Raycast(ClickRay, out ClickRayHit))
         {
             GoArray[index] = ClickRayHit.collider.gameObject;
             index++;
         }
 
     }
}
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                