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 /
avatar image
0
Question by CytricAcid · May 30, 2017 at 06:15 AM · arrayspuzzleindexof

Comparing an object with a Serializible Array with same object returns -1?

Not sure how to explain this best...

I'm working on a puzzle for my game in which the player jumps on 5 rocks in any order, and once they do, they get a reward. I've been handling this through and Event object that contains all the variables and functions needed for the puzzle, like keeping track of which rocks have been jumped on and giving the reward once all 5 are jumped on.

I am doing this through an array that contains a variable isJumpedOn, and the game object in question. I feed the game objects in through the editor, so the script can be reused multiple times in one scene instead of having a global handler, and I can set any amount of rocks to be considered jumpable.

On the rocks, when they are jumped on, they call a code in the event handler and pass themselves as a gameobject to the function that they call. in that function, I try to compare the game object they just passed to the array that I have, but it always returns -1, even though the object is in the array. I can't figure out how to fix this? I need to keep track of specific rocks because it'll be used in a similar Collectathon manner later on.

Event Handler Script:

     [System.Serializable]
     public class RocksArray
     {
         public GameObject Rock;
         public bool jumpedOn = false;
 
     }
     public Material JumpedOnMaterial;
     public bool mothSpawned;
 
     public RocksArray[] ListOfRocks;
 
 ...
 
     public void JumpOnRock (GameObject other)
     {
         int rockIndex = System.Array.IndexOf(ListOfRocks, other);
         print (rockIndex);
         //ListOfRocks [rockIndex].jumpedOn = true;
         other.GetComponent<MeshRenderer>().material = JumpedOnMaterial;
     }


Rock Script:

 void OnTriggerEnter () {
     print ("hi");
     RockJumpScript.JumpOnRock (gameObject);
 }

When I jump on a rock, it should compare itself to the game objects in the array and find which one is itself and give me the index, but instead it's returning -1.

Edit: I tried to test more directly if any of the rocks match the indexes of the array with DifferenceEquals

         if (GameObject.ReferenceEquals (ListOfRocks [1], other)) {
             print ("they match!");
         } else {
             print ("these objects are different");
                 }

But it always returns "these objects are different" even when I jump on the rock that's in index 1. So for some reason, unity doesn't recognize the object in the array with the object in the scene, even though the array is referencing the object in the scene.

Edit 2: if i replace ListOfRocks[1] with ListOfRocks[1].Rock it works. I think the best solution is a for loop...

Comment
Add comment · Show 2
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 CytricAcid · May 28, 2017 at 08:01 PM 0
Share

I figured it out. i declared rockIndex outside of the function and used a for loop and compared things manually.

     public void JumpOnRock (GameObject other)
     {
         for (int i = 0; i < ListOfRocks.Length; i++) {
             if (ListOfRocks [i].Rock == other) {
                 rockIndex = i;
                 print (rockIndex);
                 JumpRockDone ();
                 break;
             } else {
                 rockIndex = -1;
             }
         }
         if (rockIndex != -1 && !ListOfRocks [rockIndex].jumpedOn) {
             ListOfRocks [rockIndex].jumpedOn = true;
             other.GetComponent<$$anonymous$$eshRenderer>().material = JumpedOn$$anonymous$$aterial;
         } else {
             Debug.LogWarning ("Rock does not exist in the array!");
         }
     }


even though i figured it out myself, I hope my answer can help someone else with the same problem.

avatar image revolute · May 30, 2017 at 08:06 AM 0
Share

FYI, you wrote wrong code. int rockIndex = System.Array.IndexOf(ListOfRocks, other); means you are comparing (RocksArray) to (GameObject) .

They cant be same since, RocksArray (a custom class) is not GameObject.

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to find x item in list? 2 Answers

How do I make an array of buttons open corresponding images on click? 1 Answer

Convert BuiltIn array to an Generic List? 1 Answer

snake problem 0 Answers

How to set parents of all objects in an 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