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 slayer29179 · Jun 22, 2013 at 05:41 PM · arrayvariables

[JS] Adding a scripts variable to a variable

Hey all! This might be a little confusing but I will try my best to explain.

I know you can add scripts to variables e.g.

 var stats : ScriptName;

And then you assign it with either more code or in the inspector.

However, what I want is to assign a variable from ScriptName to a variable on another script.

For example if Script1 had a variable named ElfStats.

Instead of adding

 var stats : Script1;

Which gets the script, I want to get the ElfStats variable by assigning it in the inspector.

I don't want to use stats.ElfStats because my plan is to have one main script which stores all the data about every character in their own variable. For example on Script1 there will be:

 var character : String[] = ["Player", "5", "6"];
 var elf : String[] = ["Elf", "3", "2"];
 var demon : String[] = ["Demon", "6", "8"];
 var ork : String[] = ["Ork", "1", "10"];

And then on each character I will assign Script2 there will be:

 var stats : ScriptWithVariable;

Which then can be accessed from Script2:

 rigidbody.AddForce(transform.forward * stats[5]); 
 //It will be just stats, because the stats variable will contain the other variable.

This way, when you are programming a characters stats, you only need one script, so you are not looking through many and each one find its corresponding variable.

So you are literally assigning a variable from another script, as a variable.

I really hope you understand me and maybe can help me! Thank you all :)

Comment
Add comment · Show 7
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 create3dgames · Jun 25, 2013 at 01:54 AM 0
Share

Probably you should use GetComponent

avatar image Jessy · Jun 25, 2013 at 03:30 AM 0
Share

Your plan is not good. A better plan was invented long ago: Object-Oriented Program$$anonymous$$g. You should go learn about it and then revisit your script.

avatar image AlucardJay · Jun 25, 2013 at 06:19 AM 1
Share

I don't know why this has a negative vote, I have seen far worse questions here. Even though I don't fully understand the question, I am upvoting to remove negative vote.

avatar image AlucardJay · Jun 25, 2013 at 06:30 AM 1
Share

I was going to suggest as a complete guess Class and Inheritance, but I really don't know anything about them.

I started to type up an example, but again I really don't know about technical stuff (am completely self-taught. OOP, Inheritance, Serialization, I've tried to understand but my brain flips out!).

If I leave this as an incorrect example, maybe someone will come along and help clear the matter up.

 public class $$anonymous$$onster extends $$anonymous$$onoBehaviour
 {
     var name : String = "Creature";
     var attack : int = 5;
     var defense : int = 10;
 }
 
 public class Characters extends $$anonymous$$onster
 {
     var elf : $$anonymous$$onster;
     var demon : $$anonymous$$onster;
     
     function Start()
     {
         elf = new $$anonymous$$onster();
         elf.name = "Elf";
         elf.attack = 3;
         elf.defense = 2;
         
         demon = new $$anonymous$$onster();
         demon.name = "Demon";
         demon.attack = 6;
         demon.defense = 8;
     }
 }

Currently this example gives the error : Class and Inheretance/Characters.js(11,26): BCE0031: Language feature not implemented: Ambiguous($$anonymous$$onster.name, UnityEngine.Object.name).

Anyway, at least this serves as a bump.

avatar image Benproductions1 · Jun 25, 2013 at 09:24 AM 1
Share

I just came here... saw var character : String[] = ["Player", "5", "6"]; and decided to post: Y U NO USE CLASSES :O!!!

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Zuwolf · Jun 25, 2013 at 09:37 AM

If i understand, the goal is to store all your characters stats in one script. So why don't you put a same tag on your elfs for example and do GetComponent(script). You will get the script variable and you will be able to get each stats of each character. Then store it in a two dimension array ?

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 slayer29179 · Jun 25, 2013 at 06:33 PM 0
Share

Thank you Zuwolf for your help! I think this is a much better way than what I originally thought of! Thank you everyone too! :)

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Adding variables from all scripts 2 Answers

[SOLVED] Problem with "foreach". 1 Answer

Access variables, objects in array in another script? 2 Answers

script communicate with each other 0 Answers

NullReferenceException - Maze Generator Script 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