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 Kebabs · Nov 28, 2013 at 08:36 PM · arraysbuiltin arrayindexing-array

Checking what slot in a built-in Array my gameobject is in

Hi

I have made a built in array which consists of materials. I have set it up so my gameobject is a different material each time the game is played.

What I need to check though is what slot in the array my gameobject is currently in and if its possible change the tag of the object when its in a certain slot. For example this code below brings up an obvious error but it explains in simple terms what I want to do...

     if ( gameObject.renderer.material = colourChanges[0] && collider.gameObject.CompareTag("Red Player Weap"))
     {
         System.Random random = new System.Random();
         gameObject.renderer.material = colourChanges[random.Next(0, colourChanges.Length)];
         print ("I have been hit by the correct colour " + collider.gameObject.tag);
     }

I am trying to say that if the enemy is currently in slot [0] of my array and its hit by something with correct tag - do something. However the error here is that I cannot...

"Cannot implicitly convert type bool' to UnityEngine.Material'"

Which makes sense but I have no idea how to solve my issue. All I want to do is say when the enemy is this material/in this slot in the array then do this.

Any help is much obliged as this has been wrapping my brain for hours now. Seriously people any help is appreciated thank you :)

Comment
Add comment · Show 1
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 bompi88 · Nov 28, 2013 at 10:41 PM 0
Share

What is in the colourChanges array? Is it a material? It's more natural to compare the materials by their names. And you are assigning, not comparing the materials in your if statement. So something like this should be more convenient: 'gameObject.renderer.material.name == colourChanges[0].name' .

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by oneslyfox · Nov 29, 2013 at 12:04 AM

  if ( gameObject.renderer.material = colourChanges[0] && collider.gameObject.CompareTag("Red Player Weap"))

Should be:

 if ( gameObject.renderer.material == colourChanges[0] && collider.gameObject.CompareTag("Red Player Weap"))

You have '=' instead of '=='. Is that the problem? Your code seems fine otherwise as far as I can tell.

Comment
Add comment · Show 12 · 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 Kebabs · Nov 29, 2013 at 04:37 PM 0
Share

Hi Thank you both for the replies. I have tried using == and it compiles fine but its not doing what I intend it to do.

The material in colourChanges[0] is red and if its hit by a red player weap - it should change material and print...

print ("I have been hit by the correct colour " + collider.gameObject.tag);

but with == it's not working...

     if ( gameObject.renderer.material == colourChanges[0] && collider.gameObject.CompareTag("Red Player Weap"))
     {
         System.Random random = new System.Random();
         gameObject.renderer.material = colourChanges[random.Next(0, colourChanges.Length)];
         print ("I have been hit by the correct colour " + collider.gameObject.tag);
     }
     
     else
     {
         print ("Wrong colour");
     }

Thats my new code

avatar image bompi88 · Nov 29, 2013 at 04:39 PM 0
Share

What is in your array colourChanges? Strings or actual materials?

avatar image Kebabs · Nov 29, 2013 at 04:43 PM 0
Share

O$$anonymous$$ so I tried this code...

 void OnTriggerEnter(Collider collider)
 {    
     if ( gameObject.renderer.material == colourChanges[0])
     {
         System.Random random = new System.Random();
         gameObject.renderer.material = colourChanges[random.Next(0, colourChanges.Length)];
         print ("I have been hit by the correct colour " + collider.gameObject.tag);
     }
     
     else
     {
         print ("Wrong colour");
     }

and no improvement. The code is not working. It seems that my gameobject is not in the array element 0 - even though in game it is the red material assigned to element [0] in my array.

Any ideas please :)

avatar image Kebabs · Nov 29, 2013 at 04:52 PM 0
Share

$$anonymous$$aterials. I assign them in the inspector ![alt text][1]

Just ignore the Red, Blue, Green and Yellow material slots above the actual array I was just testing something else [1]: /storage/temp/18696-untitled-1.jpg

untitled-1.jpg (96.9 kB)
avatar image oneslyfox · Nov 29, 2013 at 05:01 PM 0
Share

It should be:

     if ( collider.gameObject.renderer.material == colourChanges[0])
    
Show more comments

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

18 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

Related Questions

Converting to Builtin Array for #pragma strict in javascript 2 Answers

Array elements change Gameobject.tag 1 Answer

How to set up an dynamic array 1 Answer

How would I use ArrayPrefs2 to track destroyed objects and/or instantiated objects? 0 Answers

IndexOutofRangeException 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