• 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 ObviouslyInsane · Jan 13, 2012 at 09:40 AM · errorreference

How to override in javascript?

Hi all, I need to be able to override a variable in JS. In this case it is a string variable. Basically, I have a script that generates an item and gives it a random name based on an array of strings. However, in a seperate script where I have the item variables defined eg: name, value, rarity; I get an ambiguous reference error. I am assuming this is because unity doesn't know whether to use the name I assigned, or the randomly generated name from the string array. If someone could give me a small example of how to do an override in JS i'm sure I could figure the rest out on my own.

Thank you and sorry if this is confusing. If you need any more info or code just ask and I will provide.

Comment
Add comment · Show 4
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 syclamoth · Jan 13, 2012 at 11:18 AM 0
Share

Can you show us the relevant class declarations? I think I have an idea what's going on here, but I can't be sure unless I see the line that the error is occurring on.

avatar image ObviouslyInsane · Jan 13, 2012 at 12:14 PM 0
Share
 Item.js
 
 enum Rarity{Common, Uncommon, Rare, Beastly, Insane, Epic, Legendary}
 enum ArmorSlots {Head, Neck, Shoulders, Arms, Forearms, Hands, Torso, Waist, Legs, Feet}
 
 var name : String;
 var description : String;
 var rarity : Rarity;
 var curDura : int;
 var maxDura : int;
 var _value : int;
 
 var icon : Texture2D;    
 
 function Item() {
 
     name = "Name $$anonymous$$e";
     _value = 0;
     rarity = Rarity.Common;
     maxdura = 50;
     curDura = maxDura;
 }
 
  function ToolTip() { //public virtual string
 
          return name + "/n" +  <<<ERROR OCCURS HERE
 `            "Value" + _value + "/n" +
          "Durability" + curDura + "/" + maxDura + "/n";
 }

 ItemGenerator.js
 
 var item : Item;
 var WeaponScript;
 var _weapon;
 var temp;
 
 var BASE_$$anonymous$$ELEE_RANGE : int = 1;
 var BASE_RANGED_RANGE : int = 5;
 
 var $$anonymous$$ELEE_WEAPON_PATH : String = "Item Icons/Weapon/$$anonymous$$elee/";
 var weaponNames : String[];
 
 function Start() {
 }
 
 function Update () {
 }
 
 function CreateItem() {  
  
 //CreateWeapon();
 //Decide what type of item to make
 //Call the method to create that base item type
 item = CreateWeapon();
 
 //private string _name;
 item._value = Random.Range(1, 101);
 item.rarity = Rarity.Common;
 item.maxDura = Random.Range(50, 61);
 item.curDura = item.maxDurab;
 
 return item;
 }
 
 function CreateWeapon() {
 //Decide if we make a melee or ranged weapon
 _weapon = Create$$anonymous$$eleeWeapon();
 
 //Return the weapon created
 return _weapon;
 }
 
 function Create$$anonymous$$eleeWeapon() {
 _weapon = new WeaponScript();
 
 weaponNames = new String[3];
 weaponNames[0] = "Short Sword";
 weaponNames[1] = "$$anonymous$$orningstar";
 weaponNames[2] = "Silifi";
 
 //Fill in all of the values for that item type
 _weapon.name = weaponNames[Random.Range(0,weaponNames.Length)];
 
 //Assign the max damage of the weapon
 _weapon.maxDamage = Random.Range(5,11);
 //_weapon.DamageVariance = Random.Range(.2f, .76f);
 _weapon.dmgType = DamageType.Slash;
 
 _weapon.maxRange = BASE_$$anonymous$$ELEE_RANGE;
 
 //Assign the icon for the weapon
 _weapon.Icon = Resources.Load($$anonymous$$ELEE_WEAPON_PATH + _weapon.Name) as Texture2D;
 
 return _weapon;
 }
 
 enum ItemType { Armor, Weapon, Potion, Scroll }
         

That's the Item.js and ItemGenerator.js scripts. I marked the line in Item.js where the error occurs.

avatar image save · Jan 13, 2012 at 12:24 PM 1
Share

Just a wild guess, shouldn't it be "backslash n" if you want a new line? (can't write it here)

avatar image syclamoth · Jan 13, 2012 at 01:51 PM 0
Share

You shouldn't be using the constructor for components. Where you have the constructor, you should use Awake() and AddComponent(typename). I'm actually not sure if that's exactly what's causing the problem, but it's an issue waiting to happen.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Script decides not to run anymore 1 Answer

"Object Reference not set to an instance of an object" 1 Answer

NullReferenceException - With Network PlayerSpawn 0 Answers

Null Reference Exception Error 1 Answer

NullReferenceException: object not in inspector? 0 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