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 Youngapprentice · Dec 02, 2011 at 01:37 AM · variables

Get Variable From A Script Attached To a Prefab

Hi, all! I like to make my scripts modular... I like to make one script that works for all instances and objects that might use it so that I simply have to set up parameters for it. That's why I love Unity ;)

I have a script attached to a prefab. Four different prefabs will have this script, but the variable 'x1' in each prefab will be different. From a script attached to the main camera, I would like to get the variable 'x1' from the script from Each Prefab. I do not know how to do this, though, because the prefab may not be spawned. I just need it to automatically reference the prefab, and then the script attached to it as a component, and then get the variable for each of the four. How would I go about doing this?

Thanks- YA. (Please tell me if I made this over complicated :P )

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by jahroy · Dec 02, 2011 at 02:40 AM

I think the answer to your question depends on how your prefabs exist in your scene in relation to the other objects.

  • Will they have a name that you can predict?

  • How will they be created?

  • Is there a reason why you can't attach your prefabs to your camera script directly?

It's hard to say (without knowing a little more about your scenario), but I would normally say the easiest way would be the last option.

If the script with the x1 variable is a file named SomeFunkyScript.js, you can add the following to the top of your camera script (the script that is attached to your camera):

/* this can be any object with a SomeFunkyScript attached to it. */ /* you can assign it in the Inspector by dragging and dropping */

public var funkyObject : SomeFunkyScript;

/ you can now reference funkyObject's variables and functions /

function Update () { if ( funkyObject != null ) { Debug.Log("Funky Object x1: " + funkyObject.x1); }

 else {
     Debug.LogWarning("Funky Object is null.");
 }

}

.

.

.

.

Better yet, you could declare an array of SomeFunkyScript objects like this:

public var funkyObjectArray : SomeFunkyScript [];

function Update () { var count : int = 0;

 /* print x1 value for each item in the array */

 for ( var thisFunk : SomeFunkyScript in funkyObjectArray ) {
     Debug.Log("Item " + count + " x1 value: " + thisFunk.x1);
     count ++;
 }

}

This would allow you to drag and drop multiple objects onto the slot in the Inspector named Funky Object Array.

You can drag any GameObject onto the slot that has a SomeFunkyScript script attached to it.

Hope this helps.

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
avatar image
0

Answer by Youngapprentice · Dec 02, 2011 at 02:47 AM

Thank you. Yes, every prefab is attached to the camera, because I use the variable to instantiate this. Will try it out and say what happens! Thanks! (hopefully :P)- YA

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
avatar image
0

Answer by Youngapprentice · Dec 02, 2011 at 03:14 AM

Thank you much! It worked out just fine. Works perfectly, actually! ;)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

Can one use of a static variable, change it globaly? 2 Answers

Showing GUI by pressing diffrent buttons? 1 Answer

Trying to get score counter... 2 Answers

Difficulty Calling Variables from other scrtipts 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