Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Mar 27, 2018 at 12:01 AM by cumulowinter for the following reason:

i... kinda made it work? i don't know what's going on anymore

avatar image
0
Question by cumulowinter · Mar 26, 2018 at 06:42 PM · gameobjectarraynull

Array element coming as not null

i'm trying to make an inventory system using an array of gameobjects, and i'm using a bool to keep track if the inventory is full or not, but the "if [2] !=" always comes as true, even though the space is null. this is my code, idk what to do

 void Update()
 {
     if (sylladex [2] != null) {
         isFull = true;
     } else {
         isFull = false;
     }
      }

Comment
Add comment · Show 8
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 RahulOfTheRamanEffect · Mar 26, 2018 at 06:51 PM 1
Share

Can we see the full script? It's difficult to deter$$anonymous$$e the issue with just this much.

avatar image cumulowinter RahulOfTheRamanEffect · Mar 26, 2018 at 11:13 PM 0
Share

that last bit there, the bool, is not really necessary, i was just desperate

still am Edit: I can't see the script i posted on this reply anymore, is it there still? whats going on

avatar image cumulowinter RahulOfTheRamanEffect · Mar 27, 2018 at 08:16 AM 0
Share

public class Sylladex : $$anonymous$$onoBehaviour {

 public string[] sylladex = new string[3];
 public bool isFull = false;


 public void AddItem(string item)
 {
     if (sylladex [0] == null) {
         sylladex [0] = item;
     } else if (sylladex [1] == null) {
         sylladex [1] = sylladex [0];
         sylladex [0] = item;
     } else if (sylladex [2] == null) {
         sylladex [2] = sylladex [1];
         sylladex [1] = sylladex [0];
         sylladex [0] = item;
     } else {
         //drop [2]
         sylladex [2] = sylladex [1];
         sylladex [1] = sylladex [0];
         sylladex [0] = item;
     }
 }
 public void DropItem()
 {
     Instantiate (Resources.Load(sylladex [0]), transform.position, Quaternion.identity);
 }

 void Update()
 {
     if (sylladex [0] == null && sylladex [1] != null) {
         sylladex [0] = sylladex [1];
         sylladex [1] = null;
         if (sylladex [2] != null) {
             sylladex [1] = sylladex [2];
             sylladex [2] = null;
         }
     }
     if (sylladex [2] != null) {
         isFull = true;
     } else {
         isFull = false;
     }
     {
         Debug.Log (sylladex [2]);
     }
 }

}

here's the script, sorry for the delay, only noticed this comment now

avatar image RahulOfTheRamanEffect cumulowinter · Mar 27, 2018 at 09:10 AM 0
Share

It's no problem. From the question being closed, it looks like you got it to work?

I'll detail my thoughts anyway:

  1. When you create a public array in Unity, an instance of it gets created by the Unity Serialization System (which is why you don't have to initialize the array).

  2. When an array is initialized by the Unity Serializer, it gets automatically populated with default values for that type. (The default value of a string is an empty string, which is not the same as null)

  3. Since you are managing the array only using Add Item and Delete Item, you can choose to make the array private. This way, the Unity Serializer will not modify your array (you will also not be able to see it in the inspector for your script). This way, you won't have a null issue.

  4. To take it further, I recommend you use a List structure for the sylladex, ins$$anonymous$$d of an array. A list makes it fairly easy to add or remove items and move others.

Here's a full example:

 using UnityEngine;
 using System.Collections.Generic;
 
 public class Sylladex : $$anonymous$$onoBehaviour
     {
         public int itemLimit = 2;
         private List<string> sylladex = new List<string>();
         private bool isFull = false;
 
 
         public void AddItem(string item)
         {
             if (isFull)
             {
                 if (sylladex.Count > 0)
                 {
                     //Drop the oldest item
                     string latestItem = sylladex[0];
                     Instantiate(Resources.Load(latestItem), transform.position, Quaternion.identity);
 
                     //Remove the oldest item from the list
                     sylladex.RemoveAt(0);
                 }
             }
 
             //Add a new item to the end of the list
             sylladex.Add(item);
 
             //If the number of items in the sylladex equals the item limit, set isFull = true
             isFull = sylladex.Count == itemLimit;
         }
 
         public void DropItem()
         {
             //If there are no items, do nothing
             if (sylladex.Count == 0)
                 return;
 
             //Drop the item that was added last
             int indexOfLatestItem = sylladex.Count - 1;
             string latestItem = sylladex[indexOfLatestItem];
 
             Instantiate(Resources.Load(latestItem), transform.position, Quaternion.identity);
 
             //Remove the latest item from the list
             sylladex.RemoveAt(indexOfLatestItem);
 
             //If the number of items in the sylladex equals the item limit, set isFull = true
             isFull = false;
         }
     }

There are more optimal ways of doing this, but this should be plenty to get you started. I also recommend reading up on Queues and Stacks for your data structures.

Hope that helps!

avatar image cumulowinter RahulOfTheRamanEffect · Mar 27, 2018 at 08:16 AM 0
Share

public class Sylladex : $$anonymous$$onoBehaviour {

 public string[] sylladex = new string[3];
 public bool isFull = false;


 public void AddItem(string item)
 {
     if (sylladex [0] == null) {
         sylladex [0] = item;
     } else if (sylladex [1] == null) {
         sylladex [1] = sylladex [0];
         sylladex [0] = item;
     } else if (sylladex [2] == null) {
         sylladex [2] = sylladex [1];
         sylladex [1] = sylladex [0];
         sylladex [0] = item;
     } else {
         //drop [2]
         sylladex [2] = sylladex [1];
         sylladex [1] = sylladex [0];
         sylladex [0] = item;
     }
 }
 public void DropItem()
 {
     Instantiate (Resources.Load(sylladex [0]), transform.position, Quaternion.identity);
 }

 void Update()
 {
     if (sylladex [0] == null && sylladex [1] != null) {
         sylladex [0] = sylladex [1];
         sylladex [1] = null;
         if (sylladex [2] != null) {
             sylladex [1] = sylladex [2];
             sylladex [2] = null;
         }
     }
     if (sylladex [2] != null) {
         isFull = true;
     } else {
         isFull = false;
     }
     {
         Debug.Log (sylladex [2]);
     }
 }

}

posting it again just because

avatar image bobisgod234 · Mar 26, 2018 at 11:37 PM 1
Share

Is this an array of GameObjects, or an array of Strings?

Do you ever assign null to sylladex[2] anywhere in your code?

avatar image cumulowinter bobisgod234 · Mar 26, 2018 at 11:45 PM 0
Share

It's an array of gameobjects, i only assign null to sylladex [2] on some other instance that is actually working,

 void Update()
 {
     if (sylladex [0] == null && sylladex [1] != null) {
         sylladex [0] = sylladex [1];
         sylladex [1] = null;
         if (sylladex [2] != null) {
             sylladex [1] = sylladex [2];
             sylladex [2] = null;
         }
     }
     if (sylladex [2] != null) {
         isFull = true;
     } else {
         isFull = false;
     }
         Debug.Log (sylladex [2]);
 }

after this function happens, it correctly displays that [2] is null... but that only happens after an gameobject enters and leaves that index

EDIT: wait sorry i forgot, it's an array of strings, it used to be gameobjects, changed it today

avatar image bobisgod234 cumulowinter · Mar 27, 2018 at 12:09 AM 0
Share

is the "sylladex" array declared public? If so, Unity will initialize it with empty strings (which are not null), since the Unity Serialize doesn't support nulls.

2 Replies

  • Sort: 
avatar image
1

Answer by Martin_Gonzalez · Mar 26, 2018 at 07:02 PM

Try doing Debug.Log(sylladex[2]) to see what is containing.

Comment
Add comment · Show 7 · 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 cumulowinter · Mar 26, 2018 at 10:17 PM 0
Share

the log comes out as [ ] Unity.Engine.Debug.Log:(Object) without the brackets, it just comes out as an empty text

avatar image Martin_Gonzalez · Mar 26, 2018 at 10:19 PM 1
Share

So there is an object but its empty, if not it will log null.

Can you post more of your code?

avatar image cumulowinter · Mar 26, 2018 at 10:23 PM 0
Share

So, it seems to be correctly noticing that the index is empty now, but only because i erased all of the lines of my inventory code. I readded it to print here

 public void AddItem(string item)
 {
     if (sylladex [0] == null) {
         sylladex [0] = item;
     } else if (sylladex [1] == null) {
         sylladex [1] = sylladex [0];
         sylladex [0] = item;
     } else if (sylladex [2] == null) {
         sylladex [2] = sylladex [1];
         sylladex [1] = sylladex [0];
         sylladex [0] = item;
     } else {
         //drop [2]
         sylladex [2] = sylladex [1];
         sylladex [1] = sylladex [0];
         sylladex [0] = item;
     }
 }
avatar image cumulowinter · Mar 26, 2018 at 10:24 PM 0
Share

the problem with this section of the code was on the last one, it was suposed to be activated only when [2] was full, and do an instantiate to spawn the [2] gameobject prefab, but ins$$anonymous$$d it was happening everytime i tried to pick an item up

avatar image cumulowinter · Mar 26, 2018 at 10:31 PM 0
Share

i readded those lines and did the Debug.Log again, still comes out as empty, but it triggers the bool to true, still. (bool was suposed to trigger only when [2] != not null)

Show more comments
avatar image
0

Answer by cumulowinter · Mar 26, 2018 at 10:35 PM

Ah also it seems that if i pick stuff up until there is something on [2], and drop it, it now recognizes that [2] != null

Comment
Add comment · Show 19 · 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 Martin_Gonzalez · Mar 26, 2018 at 10:39 PM 1
Share

What you are doing seems to be a Queue. You really need the index 2? Or you are saying that the number 2 is the last item of the array?

avatar image Martin_Gonzalez · Mar 26, 2018 at 10:47 PM 1
Share

No problem, we all started once.

Tell me who calls AddItem and when

avatar image Martin_Gonzalez · Mar 26, 2018 at 10:57 PM 1
Share

Try ins$$anonymous$$d of passing current.GetComponent().name, send any string like “item” just the check that that part of the code is not the problem

avatar image Martin_Gonzalez · Mar 26, 2018 at 11:04 PM 1
Share

Try this

 void Update() { 
 if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.E) && currentInterObj) 
 { 
   if (currentInterObjScript.isPickable) {
      sylladex.AddItem (“item”);
      currentInterObj.Send$$anonymous$$essage ("PickUp"); 
      currentInterObj = null; 
  } 
 }
avatar image Martin_Gonzalez · Mar 26, 2018 at 11:08 PM 1
Share

Wait me 15 $$anonymous$$, im returning home :P

Show more comments

Follow this Question

Answers Answers and Comments

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

Related Questions

how to check for GameObject is null in array with random 1 Answer

How to access gameobject array set in inspector? 2 Answers

Simple Looping drag and drop game 1 Answer

I have an error, script should check for null or not destroy game objects 2 Answers

How to check if a specific gameobject is in the array? 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