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
2
Question by Clw · Jun 23, 2013 at 03:54 PM · gameobjectprefabinstancepublic

How can I reference in a prefab's script a gameObject found in the scene

Apparently it lets me only reference other prefabs, but I am instantiating(don't know if i'm using the word correctly, i'm referring to making gameObjects out of the prefab, like so ):

 bullet = (GameObject)Instantiate (Resources.Load("Projectile"));

Is there a way to specify in the same code the parameter( it isn't really a parameter, it is the public variable whose value we have to define), for a specified instance of the prefab, something like this ?

 bullet.shoot.target = X;

In this example, shoot is the script attached to the prefab that bullet is an instance of, target is the public variable inside the shoot script, x is the name of the gameObject that we would use as a public variable in the script. The second part of the question is if there is a way of getting a list of all the gameObjects from a scene, being able to select which one you want by tags or by name, like so:

 bullet.shoot.target = MainScene.getGameObject("Target");
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
5
Best Answer

Answer by Em3rgency · Jun 23, 2013 at 06:52 PM

 //You can find object by name and access the public functions and/or variables like this:
 
 GameObject myObject = GameObject.Find("ObjectName");
 myObject.GetComponent<scriptName>().function();
 
 //For a tag search
 
 GameObject player = GameObject.FindWithTag("Player");
 player.GetComponent<OtherScript>().DoSomething();

http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html The script reference page is an awesome place ;)

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 Clw · Jun 23, 2013 at 08:06 PM 0
Share

Thanks for the response. Looking back, the way I worded my concern yielded me no results. Regardless, thanks a lot, that is exactly what I needed!

avatar image Em3rgency · Jun 23, 2013 at 08:09 PM 0
Share

I suggest using the script reference page. It has an awesome search function, where you type in a keyword and it spits out all methods with that word. Searching for "GameObject" would've even found you that function :)

avatar image AlucardJay · Jun 24, 2013 at 01:18 AM 0
Share

This is the only way you can do it with Resources Load. If you stored a reference to your projectile prefab and instantiated that, you could typecast and instantiate straight away as a script, then access its variables without GetComponent :

 public ProjectileScript projectile; // drop your prefab in the inspector here
 
 private ProjectileScript bullet;
 
 void SomeFunction()
 {
     bullet = Instantiate( projectile, transform.position, Quaternion.identity );
     bullet.shoot.target = X;
 }

  • http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

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

17 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

Related Questions

Affect All Instances Of A Prefab 3 Answers

Setting public GameObject to a different Prefab through code 0 Answers

How can I modify a variable on an instance of a prefab after I created it? 2 Answers

Save a gameobject as prefab on button press.,Is there a way for the player to create a prefab with a button press? 3 Answers

Issues with tracking prefab instances and gameobject naming (JS) 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