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 Placus_Brutus · Jul 21, 2013 at 04:40 AM · referencemultiple objects

accessing a script that is on multiple objects

This is a little difficult to explain so I'll do my best.... I have several of the same object with the same script, lets say ScriptA. From another script, ScriptB, I want to be able to call a function from ScriptA that will return the position of each of the objects in C#.... Thank you.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by robertbu · Jul 21, 2013 at 04:59 AM

You can use Object.FindObjectsOfType(). Note it is slow, so you don't want to be using every frame, and it does not find inactive game objects.

 ScriptA[] scripts = Object.FindObjectsOfType(typeof(ScriptA)) as ScriptA[];
 for (int i = 0; i < scripts.Length; i++)
     Debug.Log (scripts[i].gameObject.transform.position);

If you want inactive game objects as well, see Resources.FindObjectsOfTypeAll()

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 Dougbott · Jul 21, 2013 at 05:14 AM 0
Share

Another possible solution close to that would be using the tags ins$$anonymous$$d. I have read that it is slightly less "expensive".

avatar image robertbu · Jul 21, 2013 at 05:20 AM 0
Share

Using tags is significantly less expensive.

avatar image superluigi · Jul 21, 2013 at 05:23 AM 0
Share

I want to second Dougbott's suggestion. You could just put every object that's going to have script A attached under the same Tag and in script B use GameObject.FindGameObjectsWithTag.

avatar image
0

Answer by IgorAherne · Jul 21, 2013 at 07:02 AM

Take your time and read the whole thing, you will pick up an interesting thing from it.

Let's say I would want to access variableA in ScriptA that is a component of object with name "IgorAherne". I would like to trace a path to that script and store it in a shortcut variable _scriptA

 ScriptA _scriptA; //we've declared a shortcut variable that can accept any value that is in the form ScriptA. 
 
 _scriptA = GameObject.Find("IgorAherne").GetComponent("ScriptA"); //we search the object by name
 
 

After we found such object, ask the result of this search to continue the search in that GameObject, and search for ScriptA. It then searched in that object for the ScriptA.

After we've reached that Object's ScriptA, we want to assign that ScriptA to our prepared shortcut variable. It is possible, since shortcut was declared with type ScriptA, remember?

now you can refer to the script with just mentioning our shortcut variable, _scriptA. We will not be calling expensive function "Find" anymore, since the computer already found the script. If you look back to the beginning, I wanted to access the variableA in that script. I can do it like this:

 _scriptA.variableA 

accessing our defined script with shortcut variable and going inside it to access variableA. will only work if variableA actually exists in that ScriptA.

Finally, notice the following. I knew I was searching for a variable since the beginning. Why didn't I continue the search up to the variable and assign it to shortcut?

Well, that wouldn't have created a path to the script, but would only copy a VALUE of the variable into our shortcut. Ending the search on the place where there is no value available, such as on ScriptA means that you want to create a shortcut. otherwise you simply equalize the shortcut to the variable's value.

And that wouldn't work anyway, since in our declaration of shortcut, the shortcut was of type ScriptA, not the variable's type whatever it would be (for example Int). the shortcut of type ScriptA couldn't take the value of type int and we would get an error.

Igor Aherne

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 robertbu · Jul 21, 2013 at 07:06 AM 0
Share

@IgorAherne - while what you say is correct, it does not solve the problem outlined in the question. He has the same script attached to many game objects. He want to somehow get the position of all the game objects that have that script. So the issue is how to find all the game objects that have a specific script attached.

avatar image IgorAherne · Jul 21, 2013 at 07:09 AM 0
Share

Damn, I got to far away with explanations sry xD

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

18 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I call a function in another gameObject's script? 5 Answers

Explanation of slerp example in reference 1 Answer

Call Functions Across Scripts, Null Object Error 1 Answer

Referencing variables in another script on another object 2 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