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 Starks72 · Apr 22, 2018 at 10:05 PM · collisionrendererif-statementscolor changemousedown

How to change the material color of an Instatiated prefab when the player enters the collision area and presses a button?

I've used video tutorials and the unity documentation for everything in my game. Even though it mostly all adds up to more questions, the result are well worth it. I need additional help with this issue.

First off I receive no error codes. the function of my script is : As the player enters the box collider collision area of the gameobject and has pressed the right mouse button, change the set default color of the gameobject to a new color. Because This has not worked I also have not been able to set the conditions for ONmouseUP. Which would be keep the new set color the same after release.

The script is attached to the gameobject and not the player gameobject. Please tell me if I haven't given enough info. Thank you. using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ChangeColor : MonoBehaviour {

 public GameObject idelBlock;    //REF to bloack to be changed
 public Renderer rend;   //REF to the render to change colors
 private Color startColor;   //REF to the defualt color of Instantiated block
 
 //REF to player so Block knows what is trying to change its color
 public GameObject player; 
  
  
 
 public float distance;   //the colision range to active
 public bool inActivationRange = false;  //is in range??? yes or no
 
 


 void Start()
 {
     player = GameObject.FindWithTag("Player");

     if( player == null)
     {
         Debug.Log("Player is not tagged");
     }
     
     idelBlock = this.gameObject;
     rend = GetComponent<Renderer>();
     startColor = rend.material.GetColor("_Color");
     
 }

 void Update()
 {
     distance = Vector3.Distance(idelBlock.transform.position, player.transform.position);
 }
 
 //if player ckicks R/mouse and the distance is correct fro activation range
 //change the defualt "start color" to different color.
 //if cliked a again <= 4secs color cant be changed until after 4sec
 public void OnMouseDown()
 {
     if (Input.GetMouseButtonDown(1))
     {
         if(distance <= 1.5f)
         {
             inActivationRange = true;
            startColor =  GetComponent<Renderer>().material.color = Color.red;

         }
     }


 }
 //when the the click is released keep color as the new set color
 //when click is up within 4sec again Print "You have to wait 4Seconds"

 public void OnMouseUp()
 {
     
 }

}

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

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

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

Related Questions

How to make an object appear after collision with an invisible object. I have tried a lot of ways, but still failed please help! 1 Answer

How to change colors in turn? 1 Answer

Only detect collision once, if statement. C# 1 Answer

My Tag Functions doesn't work at all. 0 Answers

Making Objects Invisible by Using renderer.enable does not work 5 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