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 devcutters · Oct 20, 2014 at 04:32 AM · classinventoryclassesitem

About Classes in js

hello i am making an inventory system and id like to know a little bit more about classes.

so here is my question i have an item scripts i got from tutorials and im trying to understand exactly how to use class .

i have this script here

item

 public class Item
 {
     var Name : String;
     var Description : String;
     var icon : Texture2D;
     var equipped : boolean;
     var Stamina : int;
     var Power : float;
     var attackSpeed : float;
     var rarity : Rarity;
     var itemType : ItemType;
     
     function ApplyStats ()
     {
         if(equipped)
         {
             Debug.Log("Add Stats");
         }
         else
         {
             Debug.Log("Remove Stats");
         }
     }
 }
 
 
 
 
 
 
 
 enum Rarity 
 {
     uncommon,
     common,
     rare,
     epic
 }
 
 enum ItemType 
 {
     helm,
     shoulders,
     gloves,
     pants,
     boots,
     ring,
     neckless
 }

so how would i call this to instantiate a new object.then the new object would have those variables right ? and wtv on the class.

also feel free to post any relevant informative links ive looked up alot of tutorials and not many talks about classes in javascript they are pretty much all in c# is there a reason for that ? or both are as good ?

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

2 Replies

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

Answer by devcutters · Oct 20, 2014 at 11:19 PM

ok i get that things i want to know heres the situation i understand how the script works but here i have my inventory script. If you see in my function start i add my object to the maininventory but i have my drops that spawn when a tree is cut with the anim and everything thing is how would i make my actual logs and branch and coconut and actual item out of the item script like i know i can manipulate my class by calling it with class.variablename if im right but yeah id like to know a bit more about this or information about it.

i know i would have to add more item types and everythings like i said i know how the script work just that part is greyish :S

would i have to basicly make a function inside my inventory like item pickup and then make it if i go over the colider tagged item lets say with another colider tag player then add to inventory ?

 import System.Collections.Generic;
 
 
 //holding all items
 var items : Item[];
 
 //inventory
 var MainInventory : List.<Item> = new List.<Item>();
 
 //Equip menu 0 = helm 1=shoulder 2=gloves 3=pants 4=boots 5=ring 6=neckless
 var EquipMenu : Item[];
 
 //Stats
 var Power : int = 0;
 
 function Start()
 {
     MainInventory.Add(items[0]);
     MainInventory.Add(items[1]);
     MainInventory.Add(items[2]);
     EquipMenu[0] = null;
 }
 
 function OnGUI()
 {
     for(var x = 0; x < MainInventory.Count; x++)
     {
         if(GUI.Button(Rect(Screen.width/2, Screen.height/2 + (20 * x), 100, 20),GUIContent ( MainInventory[x].Name, 
         "Stamina: " + MainInventory[x].Stamina + "\n" + 
         "Power: " + MainInventory[x].Power + "\n" +
         "Attack Speed: " + MainInventory[x].attackSpeed + "\n" +
         "Rarity: " + MainInventory[x].rarity + "\n" )))
         {
             if(MainInventory[x].itemType == MainInventory[x].itemType.helm)
             {
                 if(EquipMenu[0] != null)
                 {
                     MainInventory.Add(EquipMenu[0]);
                 }
                     EquipMenu[0] = null;
                     EquipMenu[0] = MainInventory[x];
                     Power += EquipMenu[0].Power;
                     MainInventory.RemoveAt(x);
                     Debug.Log(Power);
                 
             }
             else if(MainInventory[x].itemType == MainInventory[x].itemType.shoulders)
             {
                 EquipMenu[1] = MainInventory[x];
                 MainInventory.RemoveAt(x);
             }
         }
         GUI.Label(Rect(Screen.width/2 + 150, Screen.height/2 + (20 * x), 300, 300), GUI.tooltip);
         
         var p : int = 0;
         
         if(MainInventory[x].itemType == MainInventory[x].itemType.helm)
         {
             p = 0;
         }
         
         var tooltip2 = 
         "Stamina: " + EquipMenu[p].Stamina + "\n" +
         "Power: " + EquipMenu[p].Power + "\n" +
         "Attack Speed: " + EquipMenu[p].attackSpeed + "\n" +
         "Rarity: " + EquipMenu[p].rarity;
         //second tooltip to compare to equipped item
         if(GUI.tooltip != "")
         {
             GUI.Box(new Rect(Screen.width/2 + 350, Screen.height/2 + (20 * x), 200, 300),"");
             GUI.Label(new Rect(Screen.width/2 +350, Screen.height/2 + (20 * x), 300, 300), tooltip2);
         }
     }
     
     for(var y = 0; y < EquipMenu.length; y++)
     {
         if(EquipMenu[y] != null)
         {
             if(GUI.Button(Rect(Screen.width/2 - 150, Screen.height/2 + (20 * y), 100, 20)," " + EquipMenu[y].Name))
             {
                 if(EquipMenu[y] != null)
                 {
                     MainInventory.Add(EquipMenu[y]);
                     Power -= EquipMenu[y].Power;
                     EquipMenu[y] = null;
                 }
             }
         }
     }
 }

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 devcutters · Oct 28, 2014 at 08:30 PM 0
Share

here a nice tutorial i found out there that explain and complete jesseEtzler https://www.youtube.com/playlist?list=PLxLNqnnCshwm9ayxq$$anonymous$$peWH4P1SLzrN92-

here is the link to the tutorial : https://www.youtube.com/watch?v=$$anonymous$$6gUSw5wl3Q

alot of information about classes figured i post it here since theres not alot of view on this guy video and that pretty much handled all the questions i had after jesse tutorials

avatar image
0

Answer by Phantomized · Oct 20, 2014 at 06:48 AM

It's important to understand how unity treats the whole class system. Unity scripts inherit from something called MonoBehaviour which is basically the base class at the top. In JavaScript the fact that you inherit from that is hidden, but if you open a C# script you can see that your class inherit from it.

Monobehaviour Works so that it treats the script you create in the unity engine, as something that resembles a class. So you don't instantiate classes usually in unity. If you put that script on a gameobject in the hierarchy, you would actually be having an instance of your class as a Component on your gameobject. It's a very "physical" approach to instantiating the generic way.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Class Programming to build an inventory system 3 Answers

Item database with override functions 0 Answers

Implementing an Item system - Defining Items 1 Answer

inventory system: drop item with mouseclick 1 Answer

how to detect wich item i have in my inevntory 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