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 dnoparker · Sep 01, 2013 at 01:02 PM · materialif-statementsreturntrue

If statement will not return true

 using UnityEngine;
 using System.Collections;
 
 public class GreenWall : MonoBehaviour {
 public GameObject Player;
 public Material Green;
 public bool activeGreen = false;
 
         
     void Update () {
         
     if (Player.gameObject.renderer.material == Green)
         activeGreen = true;
     }
     
     void OnCollisionEnter(Collision collision) {
         if (collision.gameObject.name == "Player" && activeGreen == true)
             transform.Translate (new Vector3(3,0,0));
     
     }
 }

I am trying to check if the Player's material is called Green and if so return true on 'activeGreen' but for some reason the IF statement would not return true. I have checked the rest of the code by forcing activeGreen to be true and everything works, so I guess the problem lies in the IF statement. I have done this code with just color instead of material and it works fine. I am at a bit of a loss.

Any help would be appreciated! Thank you

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by meat5000 · Sep 01, 2013 at 01:03 PM

I don't see anywhere you actually assign the material called Green.

You are checking for a material not a colour. If you use color Green, in this case Green is a predifined Color.

By using public Material Green; you have created a Material instance called green, but as I say you have not assigned it.

renderer.material.color = Color.green;

will change the color of the material to green.

http://docs.unity3d.com/Documentation/ScriptReference/Renderer-material.html

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 TrickyHandz · Sep 01, 2013 at 02:43 PM

It appears that you are trying to access the base or SharedMaterial rather than the instance that is assigned to the renderer. Keep in mind, that each object can use the same SharedMaterial, but the material property can be manipulated at runtime so that it only affects a certain object. Make this small change to the code and see if this helps:

 void Update () {
  
     if (Player.gameObject.renderer.sharedMaterial == Green)
        activeGreen = true;
     }

NOTE: I'm assuming that you are assigning the value of the class member "Green" in the inspector for this to work.

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 citizen_rafiq · Sep 01, 2013 at 08:14 PM

public GameObject Player;

public Material Green;

set both var correctly

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.

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

Using bool function with parameters 1 Answer

Why doesn't my if statement work? 1 Answer

How to check if an objects current material is the correct one? 1 Answer

Secound Animator.GetCurrentAnimatorStateInfo(0).IsName() doesn't work in if Statement 0 Answers

Time is not stopping 2 Answers


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