• 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 Adam 2 · Jul 14, 2010 at 02:34 AM · javascriptarraylag

Opening inventory in game makes it lag really bad

so i took a script form a forum post,http://forum.unity3d.com/viewtopic.php?t=46222 , and changed to be used for me:

// var inventory : Array; static private var InventoryOpen: boolean = false; var InventoryGUI:Texture2D; var SpawnPoint: Transform; var ChatOpnener;

var doWindow : boolean = false;

//
public var emptyTex : Texture;

// (- ) public var inventorySizeX = 4; public var inventorySizeY = 5;

//
var iconWidthHeight = 40;

// ( X Y) var spacing = 4;

// public var offSet = Vector2( 100, 100 );

// (. Update()) private var itemImage : Texture2D;

//
class InventoryItem { //
var worldObject : GameObject; //,
var texRepresentation : Texture2D; }

//
function Awake() { ChatOpnener = GameObject.Find("Tank Weapon Shop Keep").GetComponent("ActiveChat"); offSet = Vector2(Screen.width/2+offSet.x,Screen.height/2+offSet.y); inventory = new Array(inventorySizeX);

 for( var i = 0; i < inventory.length; i ++ )
 {
     inventory[i] = new Array(inventorySizeY);
 }

}

function OnGUI() {

var texToUse : Texture2D; var currentInventoryItem : InventoryItem; if(InventoryOpen&&!ChatOpnener.ChatIsOpen){ GUI.Label(Rect(Screen.width/2-200,Screen.height/2-200,400,400),InventoryGUI); //
for( var i = 0; i < inventory.length; i ++ ) { //
for( var k = 0; k < inventory[i].length; k ++ ) { texToUse = emptyTex; currentInventoryItem = inventory[i][k];

         //    I-   K- , 
         if( inventory[i][k] != null )
         {
             texToUse = currentInventoryItem.texRepresentation;
         }

         var it = GUI.Button( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), texToUse );
         GUI.Button( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), texToUse );
         var w : int = 0;
         var h : int = 0;
         if (it &amp;&amp; currentInventoryItem != null) 
         { 
         print("TEST");
             Instantiate(currentInventoryItem.worldObject,SpawnPoint.transform.position,Quaternion.identity);
             currentInventoryItem.texRepresentation = null; 
             inventory[i][k] = null;


         }

             //  
         Debug.Log("item droped");
         }
     }
 }

}

function AddItem( item : InventoryItem ) { //
for( var i = 0; i < inventory.length; i ++ ) { //
for( var k = 0; k < inventory[i].length; k ++ ) { // ,
if( inventory[i][k] == null ) { inventory[i][k] = item; return; } } }

 //  ,  -   

}

function AddItem1( worldObject : GameObject, texRep : Texture2D ) { var newItem = new InventoryItem();

 newItem.worldObject = worldObject;
 newItem.texRepresentation = texRep;

AddItem( newItem );
}

function Update(){ if(Input.GetKeyDown(KeyCode.I)){ if(InventoryOpen){ InventoryOpen=false; }else{ InventoryOpen=true; } } }

but when i added this part:

if (it && currentInventoryItem != null) { print("TEST"); Instantiate(currentInventoryItem.worldObject,SpawnPoint.transform.position,Quaternion.identity); currentInventoryItem.texRepresentation = null; inventory[i][k] = null;

             }

it lagged my game REALLY bad, it must e something here, just try and help me please, if more info is needed tell me

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Tetrad · Jul 14, 2010 at 03:55 AM

You definitely don't want to have that code inside OnGUI. It runs multiple times a frame.

Comment
Add comment · Show 2 · 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 Adam 2 · Jul 14, 2010 at 04:14 AM 0
Share

what do i want it in then? LateUpdate?

avatar image Tetrad · Jul 14, 2010 at 06:12 AM 0
Share

Probably. Just make sure you're only doing it as often as you need to.

avatar image
1

Answer by Ony · Jul 14, 2010 at 07:56 AM

You just need to go through your code and separate the GUI stuff from the calculation stuff. Move the calculations into their own functions and then call them as needed when you press a button in the GUI.

Don't move it to LateUpdate. That won't solve the fundamental issue which is just simply that you need to organize your code into sections that get called when needed instead of multiple times per frame.

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
avatar image
0

Answer by Cyb3rManiak · Aug 11, 2010 at 03:26 PM

First of all - you have to go over the code and tidy it up.... Lots of wasted CPU cycles are bad for global warming :)

Second - if in fact when you add those lines it got really bad, just for kicks try and define it as a boolean and not let UnityScript figure it out by itself. See if it gives you some boost. Duck typing is fun, but has some performance loss.

var it: boolean = GUI.Button(...

instead of:

var it = GUI.Button(...
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
avatar image
0

Answer by MrLolEthan · Oct 20, 2012 at 12:28 PM

Use time.timeScale = 0 when inventory is open to freeze time. That should freeze the lag but if you want to have things moving in the background of the inventory than this is not the best option.

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.

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

1 Person is following this question.

avatar image

Related Questions

Possible to use an array value as the name of a variable? 2 Answers

Make an array containting multiple classes 1 Answer

expected. Insert a semicolon at the end. When the end that it says, is a } Could someone help us fix this? 2 Answers

have array target person of most priority/stick with top prior 0 Answers

Accessing variable from a class 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