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 $$anonymous$$ · Jan 28, 2019 at 05:25 PM · if statementswitching

want to switch between material when triggered, but facing weird problem. help.

I used this following script

 public void OnCollisionEnter(Collision other)
 {
     if(other.gameObject.tag == "Player")
     {
             if (this.gameObject.GetComponent<Renderer>().material = onn)
             {
                 this.gameObject.GetComponent<Renderer>().material = offf;
                 Debug.Log("turned off");
             }
             if (this.gameObject.GetComponent<Renderer>().material = offf)
             {
                 this.gameObject.GetComponent<Renderer>().material = onn;
                 Debug.Log("turned on");
             }
         
     }
 }  




when player collides once, this function should be carried out once. but it is carried out several times. I'm getting both "turned off" and "turned on" messages.

what are some other methods to execute this problem ??

thanks in advanced

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
3
Best Answer

Answer by Hellium · Jan 30, 2019 at 09:13 AM

 private int collisionsCount = 0 ;
 private Renderer renderer;
 
 private void Start()
 {
     // Save a reference to the renderer component for better performance
     renderer = GetComponent<Renderer>().material;
 }
 
 public void OnCollisionEnter(Collision other)
 {
     // Use CompareTag for better performances
     if(other.gameObject.CompareTag( "Player") )
     {
         collisionsCount++ ;
         if( collisionsCount % 2 == 0 ) // Check if the number of collision is odd or even
         {
             renderer.material = onn ;
             Debug.Log("turned on");
         }
         else
         {
             renderer.material = offf ;
             Debug.Log("turned off");
         }
 
         // Or, for a shorter solution
         // renderer.material = ( (++collisionsCount) % 2 == 0 ) ? onn : off;
     }
 }  





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 $$anonymous$$ · Jan 31, 2019 at 04:57 PM 0
Share

yep. thats a nice goto solution

avatar image
0

Answer by Turlok · Jan 28, 2019 at 06:35 PM

I'm not in a position to test right now, but I can see two issues with your code. First, the if conditions are actually assignment operations and not boolean conditionals so try changing:

 if (this.gameObject.GetComponent<Renderer>().material = onn)

to

 if (this.gameObject.GetComponent<Renderer>().material == onn)

Second, after switching onn to offf you're immediately checking if it's offf and changing it back to onn, so change:

 if (this.gameObject.GetComponent<Renderer>().material = offf)

to

 else if (this.gameObject.GetComponent<Renderer>().material == offf)



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 $$anonymous$$ · Jan 30, 2019 at 09:03 AM 0
Share

sorry that doesnt work, I stopped getting either of the message (turned off, turned on)

avatar image Turlok $$anonymous$$ · Jan 30, 2019 at 06:15 PM 0
Share

in that case, are onn and offf both of type $$anonymous$$aterial? Also, does the object the code is checking already have either onn or offf as it's material? It might be safer to make an enum variable representing which materials you want to apply, and then check the object's current enum to apply the desired material.

avatar image $$anonymous$$ Turlok · Jan 31, 2019 at 04:37 PM 0
Share

yes, gameobject already have onn or offf material on it.

avatar image
0

Answer by king_opping · Jan 30, 2019 at 09:13 AM

hi, what can i see here is a need more basic programming definition. because when you declare if statement it means you compare the logic, it is different between "=" and "=="

"=" is used to assign value to a variable "==" is used to compare logic

so your code i think should be like this...

  public void OnCollisionEnter(Collision other)
  {
      if(other.gameObject.tag == "Player")
      {
              if (this.gameObject.GetComponent<Renderer>().material == onn)
              {
                  this.gameObject.GetComponent<Renderer>().material = offf;
                  Debug.Log("turned off");
              }
              if (this.gameObject.GetComponent<Renderer>().material == offf)
              {
                  this.gameObject.GetComponent<Renderer>().material = onn;
                  Debug.Log("turned on");
              }
          
      }
  }  


hope it helps.

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 $$anonymous$$ · Jan 31, 2019 at 04:33 PM 0
Share

hi thanks for the reply, but it doesnt work. :(

avatar image
0

Answer by surfuay · Jan 31, 2019 at 12:22 AM

hi, if you haven't solved it yet I think the problem is the opening line

you're using OnCollissionEnter

I think what you're looking for is

OnTriggerEnter

then your following script of course you'll need to have the box "IsTrigger" checked off in each game object that is using any reference to this script. also this might be irrellevant but when i've had the parenthesis for the Collision i write mine as Collider

so

public void OnTriggerEnter(Collider other) { the rest of your code }

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 DCordoba · Jan 31, 2019 at 02:12 AM 0
Share

idk why tou think OnTriggerEnter is better for that case, and what would be changed to replace the OnCollisionEnter()

to the current purpose I think he archieve exactly the same result, please illustrate us why change that.

avatar image $$anonymous$$ · Jan 31, 2019 at 04:31 PM 0
Share

but if I checked isTrigger, player will pass right through the game object, I dont want that. I want player to collide and bounce back.

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

102 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

Related Questions

How to solve weird rotation problem? 1 Answer

Syncing files between Mac and PC seperate locations 0 Answers

Switching cameras (JavaScript) 0 Answers

No mater what I cannot make this GUI button true! please help. 1 Answer

Using "collider.name" vaule to determine next move? C# 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