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 Alkan · Oct 20, 2011 at 12:54 PM · gameobjectinstantiatearray

Instantiating gameobject variables (in an array) from a different script

I have 2 prefabs assigned to variables (placed in an array) in a script called test:

 var one : GameObject;
 var two : GameObject;
 
 static var prefabs = new Array();
 prefabs[0]= one;
 prefabs[1]= two;

I'm trying to access them with another script as follows:

 function Start () {
 var instance : GameObject = Instantiate(test.prefabs[0], transform.position, transform.rotation);
 }

..but I'm having no luck - the error in the console is "ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count."

Can anyone explain what I'm doing wrong?

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
2

Answer by BiG · Oct 21, 2011 at 08:42 AM

When you want to use a variable from another script, you have to specify that script first. However, the compiler cannot understand the script that you're trying to "inspect" on its own.

I'm supposing that the first script that you've posted is called "script1", the second one is called "script2", and script1 is assigned to an object called "my_object". So, try this (in script2):

 function Start(){
    var my_prefab=GameObject.Find("my_object").GetComponent(script1).prefabs[0];
    var instance=Instantiate(my_prefab, transform.position, transform.rotation);
 }
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 Alkan · Oct 26, 2011 at 02:53 PM 0
Share

Thanks for the explanation, I will give this a try.

Can you explain why I'm able to access other script's static numeric variables by simply writing the script's name followed by a fullstop - but in this example I need to first access the gameobject that the script is attached to and then use GetCmoponent?

avatar image BiG · Oct 27, 2011 at 08:42 AM 0
Share

Well, standing to my knowledge, you have to do as I said in the question when you access a variable in a script, attached to a certain object, from another object. If I'm wrong, I hope to be corrected by anyone.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Instantiate from array into array? 2 Answers

Save Gameobject in Array 1 Answer

Want replace a current object with one inside array the object being replaced with is prefab 1 Answer

Instantiating a gameObject in an array is not working for me 1 Answer

Referencing an instantiated object with a global variable 0 Answers


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