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 Cap · May 29, 2010 at 02:44 AM · variableloadcastmissingfieldexception

Getting variables after Resources.LoadAll

I have become completely confused by how to load variables in one script from another. Please go easy on me since I'm only just starting with Unity. I'm building a game which will include a large number of ships, so I want to have a separate script to hold various specs for every ship (speed, turning, etc). I'm loading these up via Resources.LoadAll and then trying to access the variables within them from another script, but I'm getting a 'MissingFieldException: Field 'UnityEditor.MonoScript.shipturn' not found.' error.

Here's the script (instantiator) which loads up the ships into an array:

static var ships : Object[];

static var weps : Object[];

function Awake () {

ships = Resources.LoadAll("Ships");

weps = Resources.LoadAll("Weapons");

}

This seems to work normally, I can use print(ships[0]); to return the text of the ship's script file. However, when it comes to accessing one of those variables (shipturn), using the following code, it fails:

playersship=instantiator.ships[playership.playershiptype]; // same as instantiator.ships[0]

var rotationspeed=playersship.shipturn; // errors here

I have a feeling these ship scripts are miscast, but I don't know how to cast them properly. The ship script files contain stuff like the following:

var shipname="testship";

var shipspeed=600;

var shipturn=120;

Thanks in advance for any help.

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 Lucas Meijer 1 · May 29, 2010 at 11:02 AM

Resources.LoadALl() is useful when you really need it, but in most cases best avoided. The "usual" way of achieving what you want is:

  • import a 3d model for your ship.
  • Create a script that gives it some logic, and that has some variables (speed, turnspeed, etc)
  • drag the model into your scene
  • apply the script
  • create a new prefab
  • assign the model+script in your scene to the prefab

now you can instantiate that prefab all over the place, and change speeds and turnspeeds.

Good luck, Lucas

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 Cap · May 29, 2010 at 04:24 PM 0
Share

Thanks for your response. I looked at doing things that way but I think I may well really need it in the future. It would be much easier to populate the scene with just a couple of lines of code than having to instantiate everything separately or manually put all the prefabs in an array.

avatar image Lucas Meijer 1 ♦♦ · May 31, 2010 at 11:39 AM 0
Share

I used to feel the same way, but got converted :). Please note that if you prefer code over UI actions, you can also easily write an editorscript that sets things up for you.

avatar image Cap · May 31, 2010 at 07:06 PM 0
Share

Thanks, I'm looking into the editor scripts now and they definitely seem handy.

I managed to solve this now I think, just made a generic shipspec script and dragged it onto each ship I create and then populate its variables through the UI when adding each new ship (hoping to use editor scripts for this). Using GetComponent("shipspec").variable seems to have achieved what I was after, so I'll mark this as solved :)

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

Resources.LoadAll Issue with variable type 1 Answer

Checking if int is !null not working ? Strange... 2 Answers

Instantiate Resources Load not working ... 1 Answer

Setting variable as type "Scene" 4 Answers

How to make slider load variable on start 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