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 RSud · Sep 20, 2010 at 05:14 PM · transformvariablebce0019

Transform handle for prefab and getting to a prefab script variable?

New to Unity.... Need some help with correct syntax on accessing a script variable in a prefab to which I have a transform handle.

I have code below in a "GameMaster" script. This script instantiates a prefab with a Transform handle. I want to get at a script variable that is in a script that is part of the same prefab.

The line 'badGuy.Enemy1_AI.EnemyLevel = badEnemyLevel;' in the GameMaster script fails with 'Enemy1_AI is not member of unity.transform'.

I understand this since I have declared badGuy as a Transform which is a sub-component. What would be the correct syntax to access a script variable (Enemy1_AI is the script) when I have a reference to a Transfrom that is part of the prefab?


// GameMaster.js

var enemy1 : Transform; var badGuy : Transform;

var badEnemyLevel : int =1;

badGuy = Instantiate ( enemy1, Vector3 ( (Random.value * 400) - 200, 0, -2460), Quaternion.identity);

badGuy.Enemy1_AI.EnemyLevel = badEnemyLevel;


// Enemy1_AI.js

static var EnemyLevel : int;


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
0

Answer by swhite · Sep 20, 2010 at 05:19 PM

Make sure you check out some of the reference and user manuals for Unity; you'd be surprised the quality of guides they've put together!

http://unity3d.com/support/documentation/ScriptReference/Component.GetComponent.html

var enemy1 : Transform; var badGuy : Transform;
var badEnemyLevel : int = 1;
badGuy = Instantiate ( enemy1, Vector3 ( (Random.value * 400) - 200, 0, -2460),      Quaternion.identity);
*badGuy.gameObject.GetComponent(<scriptname>).EnemyLevel=badEnemylevel;*

Comment
Add comment · Show 4 · 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 RSud · Sep 20, 2010 at 05:44 PM 0
Share

I have looked over some documentation. I had looked at the material for the link you provided. Haven't found my answer yet...

avatar image swhite · Sep 20, 2010 at 06:01 PM 0
Share

I edited my answer to show you a bit of an example; I think that's what you're looking for.

avatar image RSud · Sep 21, 2010 at 02:34 AM 0
Share

$$anonymous$$y script is as follows: badGuy.gameObject.GetComponent(Enemy1_AI).EnemyLevel = badEnemylevel;

The error I now see:

Assets/FSF_Game/Scripts/Game$$anonymous$$aster.js(76,77): BCE0019: 'EnemyLevel' is not a member of 'UnityEngine.Component'. (Filename: Assets/FSF_Game/Scripts/Game$$anonymous$$aster.js Line: 76)

How to fix?

avatar image RSud · Sep 21, 2010 at 02:39 AM 0
Share

$$anonymous$$y script is as follows:


badGuy.gameObject.GetComponent(Enemy1_AI).EnemyLevel = badEnemylevel;


The error I now see:


Assets/FSF_Game/Scripts/Game$$anonymous$$aster.js(76,77): BCE0019: 'EnemyLevel' is not a member of 'UnityEngine.Component'. (Filename: Assets/FSF_Game/Scripts/Game$$anonymous$$aster.js Line: 76)


How to fix?

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

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

How do I change the position of a variable 1 Answer

Is there a way to get rotation data for single axes? 2 Answers

Transform Position? 1 Answer

transform = transform 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