Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 e-bonneville · Apr 03, 2010 at 12:22 AM · prefabreference

Setting References (!Inspector)

How would I set references to scripts from inside a script? To clarify my problem, I have a gameObject. I'm instantiating it during runtime, and would like to have it access another script. However, I can't set references directly in prefabs, so I need a way to instantiate it with a reference, or maybe have it get a reference. Would I use gameobject.find and then use a getcomponent? Thanks a heap, and all help appreciated! (as in upvotes)

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
1
Best Answer

Answer by Eric5h5 · Apr 03, 2010 at 01:58 AM

It's not really possible to answer that definitively unless you say what other script you want to access and where it's located. Assuming that the script you want is attached to the same prefab as the first script and is called "AnotherScript", your first script would have something like:

private var aScript : AnotherScript;

function Start () { aScript = GetComponent(AnotherScript); }

But, like I said, it depends.

Edit: based on your comment, there's more than one way to do that. If there's just one object, you can do this:

private var aScript : Stock;

function Start () { aScript = GameObject.Find("Resources").GetComponent(Stock); }

Or this, if there's just one copy of the script:

private var aScript : Stock;

function Start () { aScript = FindObjectOfType(Stock); }

Or, for a different approach, you could make "clicked" on Stock a static variable:

// Stock.js
static var clicked : boolean;

That way you don't need to get a reference, and can set "Stock.clicked = true" from anywhere, at any time. Static variables are appropriate if there's only one of them in the entire game. You can use that to make Stock itself into a singleton:

// Stock.js var clicked : boolean; static var instance : Stock;

function Awake () { instance = this; }

Then, on other scripts, without having to get a reference, you can do this:

Stock.instance.clicked = true;

You can access any public variables or functions in Stock.js that way.

Comment
Add comment · Show 5 · 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 e-bonneville · Apr 03, 2010 at 02:09 AM 0
Share

@Eric5h5 Well, what I'm trying to do is instantiate a prefab, and have it access a script on a gameObject called 'Resources'. There's only one gameObject called 'Resources', and only one script called 'Stock' in the game. I need to access it from a newly created gameObject so I can modify it, so if I enter a trigger, it sets 'Resources.clicked' to true... That's the gist of it anyway. Thanks for answering!

avatar image Eric5h5 · Apr 03, 2010 at 02:30 AM 1
Share

@elbon96: O$$anonymous$$, that makes things much clearer...I added some stuff to my answer.

avatar image e-bonneville · Apr 03, 2010 at 02:33 AM 0
Share

@Eric5h5 Thx... That looks like gold!

avatar image e-bonneville · Apr 06, 2010 at 10:42 PM 0
Share

@Eric5h5 BTW, bit late, but this line in your script: aScript = GameObject.Find("Resources").GetComponent(Stock); needs to be this: aScript = GameObject.Find("Resources").GetComponent("Stock");

avatar image Eric5h5 · Apr 06, 2010 at 11:38 PM 0
Share

@elbon96: No, that would make it worse. Using GetComponent without quotes is preferable because it's faster and catches errors at compile-time ins$$anonymous$$d of run-time.

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

No one has followed this question yet.

Related Questions

Scripts of instantiated objects 1 Answer

How to assign prefab at runtime? 1 Answer

Can I get a reference (not a copy) to a string from a script? 2 Answers

Setting up reference to an inactive GameObject through script 1 Answer

How to access a child input field from a parent prefab script. 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