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
1
Question by BDY · Mar 16, 2011 at 12:27 AM · javascriptinstantiateprefab

How to access script variables attached to a prefab at runtime in Javascript?

I have a script that generates a grid of prefab tile objects at runtime. I have attached a script to the prefab that contains a single Vector3 to store the grid coordinates of each tile as it's instantiated. How do I access the script component of the prefab from the script instantiating them? I've tried a bunch of different things, but they don't work because a prefab isn't a class.

Thanks for any help.

Comment
Add comment · Show 1
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 Uriel_96 · Mar 16, 2011 at 12:41 AM 0
Share

I think the prefab to do something like script needs to be in scene so maybe you can take it to the scene then after the script does what you want to save it to your prefab and then you destroy the game object you have in scene.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by DaveA · Mar 16, 2011 at 12:39 AM

If I understand correctly, you'd do it just like any other object. Once instantiated, you can access the component (script) then anything in it: http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Components.htmland http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

The prefab isn't a class, it's an object, but you can get the component (script) off an object, thus stuff in that script.

If that doesn't do it for you, can you post some code?

Comment
Add comment · Show 1 · 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 BDY · Mar 16, 2011 at 02:26 AM 0
Share

Ah, that cleared it up, thank you!

basically I did this:

newHex = Instantiate(grassHex, Vector3(curX,curY,curZ), hexRotation); newHex.GetComponent(hexProperties).setCoords(new Vector3(j,i,k));

where hexProperties is the script component attached to the grassHex prefab.

Thanks again!

avatar image
0

Answer by Pawan Asati · Jul 18, 2013 at 05:12 AM

< YourObjec type> GirlAnimation=(Resources.Load("GirlAnimation")) as BoneAnimation;

Your prefab should be keep in assts/Resources forlder then you can use it

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

1 Person is following this question.

avatar image

Related Questions

Combining Meshes? 1 Answer

Instantiating Prefab from Javascript - BCE0005: Unknown identifier: 'Prefab' 2 Answers

Scripting a prefab to duplicate itself within a fixed radius 2 Answers

Bullet Instantiation 1 Answer

Instantiated Prefab's script doesn't work 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