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 Dreamer · Apr 28, 2011 at 05:43 AM · classinventoryitem

Class Programming to build an inventory system

I am doing inventory system, and try to implement it in Classes way. But there are some problems.

class Inventory{ private var items:Item[]=new Item[GameData.player_max_slots];

function Inventory(){ print("1"); for(var i=0;i<GameData.player_max_slots;i++) items[i]=null; }

function Print_Item_Names(){ for(var i=0;i<GameData.player_max_slots;i++) print(items[i].GetName);

 print("2");

}

function Add_Item(_item:Item) { for(var i=0;i<GameData.player_max_slots;i++) items[i]=_item; print("3"); }

}

class Item{ private var name:String;

function Item(_name:String) { name=_name; }

function GetName():String{ return name; }

}

And I try to test it:

if (Input.GetKeyDown ("q"))
{
    var player_inventory:Inventory=new Inventory();
    player_inventory.Add_Item(new Item("ff"));
    player_inventory.Print_Item_Names();
}

Print_Item_Names() doesn't work. And even print("1"); print("2"); print("3"); in methods of the inventory Class are not called.

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

3 Replies

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

Answer by TheDemiurge · Apr 28, 2011 at 09:29 AM

Classes that don't derive from MonoBehaviour don't properly recognize the print function, though I don't remember why anymore. For whatever reason they don't throw an error on it, but it just doesn't do anything. You can use Debug.Log and it will work, or you can make your class derive from MonoBehaviour - depending on your needs.

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 Dreamer · Apr 28, 2011 at 11:21 AM 0
Share

Yes, you got the point. I found print() not applicable in class methods, other methods function normal.

avatar image
0

Answer by jahroy · Apr 28, 2011 at 06:19 AM

Sounds like the constructor is not getting called.

If you put a print("0"); in front of the first line, does IT print?

Is your script attached to an object that's enabled?

Is it in an OnGUI or Update function (or something like it)?

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 Dreamer · Apr 28, 2011 at 06:38 AM 0
Share

If you put a print("0"); in front of the first line, does IT print? No, it doesn't print.

Is your script attached to an object that's enabled? 1. The 2 classes scripts doesn't attach to anything, do I need to? 2. The script of press "q" and print result is attached to an empty object, this function will execute. I have tested.

Is it in an OnGUI or Update function (or something like it)? Yes, the press "q" function is in Update();

Thanks

avatar image Dreamer · Apr 28, 2011 at 06:38 AM 0
Share

err. why no line break...maybe need to press shift+enter? I try try 1 more time

avatar image jahroy · Apr 28, 2011 at 07:24 AM 0
Share

I typically attach scripts with OnGUI() or Update() functions to the GameObjects they interact with. In your case I would attach that script to an empty, enabled GameObject for testing purposes. Or to anything in your scene.

avatar image
0

Answer by CHPedersen · Apr 28, 2011 at 06:26 AM

First of all, you don't need to initialize reference type variables to null. I.e., in your Inventory constructor, don't loop through the array, setting items[i]=null;. When you create an empty static array of objects, the elements are automatically null.

That said, I'm not sure why at least print("1") wouldn't get called. All other errors aside, when you hit q, the first thing that happens is that the Inventory constructor is invoked, which calls print("1") as its very first action. Therefore, I suspect there's a problem with this code's location in your program. Where have you put the if-statement testing for the q-key? Is it in some script's Update? Is that script enabled?

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 Dreamer · Apr 28, 2011 at 06:42 AM 0
Share

if statement will be executed, I have tested. It's in Update.

I agree constructor is invoked, theory print will run first. Very wired ahhhh~~~

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

Implementing an Item system - Defining Items 1 Answer

About Classes in js 2 Answers

Inventory armor wielding proplem,How to convert from derived to base 1 Answer

Need assistance with SubType in custom class (UnityScript or C#) 2 Answers

Making an Inventory (Basics) 2 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