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 RickTick · Jul 21, 2012 at 03:44 PM · gameobjectarrayinventoryadding

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?

Comment
Add comment · Show 3
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 RickTick · Jul 21, 2012 at 05:20 PM 0
Share

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);
  
  }
 
 }
avatar image Seth-Bergman · Jul 21, 2012 at 05:23 PM 0
Share

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.

avatar image RickTick · Jul 21, 2012 at 06:16 PM 0
Share

Yes it is and I tried it, but always I get the same error: 'Add' is not a member of 'InventoryTest'.

3 Replies

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

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.

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 RickTick · Jul 21, 2012 at 06:17 PM 0
Share

Switched to Lists, thanks!

avatar image Artifactx · Jul 22, 2012 at 06:14 AM 0
Share

Built in arrays are faster than lists but ok...

avatar image Eric5h5 · Jul 22, 2012 at 04:08 PM 0
Share

They are, yes, but how much faster depends on the type...for GameObject, there's little difference.

avatar image
0

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)

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 Eric5h5 · Jul 21, 2012 at 05:13 PM 0
Share

You can't use Add with built-in arrays.

avatar image Seth-Bergman · Jul 21, 2012 at 05:19 PM 0
Share

oops. Fixed now.

avatar image
0

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++;
         }
 
     }

}

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

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

7 People are following this question.

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

Related Questions

Setting generic list length 2 Answers

Can't add GameObjects to ArrayList 1 Answer

Array Help GameObject Length Inventory 1 Answer

HELP How to remove gameobject from Built-in Arrays 2 Answers

How to create an array of Game Objects with different tags. 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