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 MorphusOne1 · Aug 07, 2017 at 09:59 AM · c#raycastprefabmaterialarrays

How to effect and apply a common material to children individually...

Hi Guys,

I'm having trouble changing the material on children inside a prefab via a raycast hit...

To set the scene I have a city that I want to be able to click on blocks and bring up a list of some sort, all in AR.

I've got everything working for the prototype aside from the "highlight"/change material.

I want to be able to highlight/change material of the block I click on/raycast hit, as well as bring up the UI details about the block/plot/gameobject.

Eventually I want to be able to bring in those details via a CSV file and update via the cloud but need the prototype up and running first.

I've got the material changing but it effects the first material slot of everything...

Eventually I want to be able to do more things with the children but will settle for understanding my problem a bit better...

As always I appreciate your time and any and all help is much appreciated.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections.Generic;
 
 public class IsClicked : MonoBehaviour {
 
     public CanvasGroup ItemCanvas;
     public Renderer rend;
     public Material StartColour;
     public Material childColour;
     public List<Transform> objects;
     public List<Transform> childObjects;
     public Material Glow;
 
     List <Material> childColours;
     List<Renderer> childRenderers;
     Renderer childRenderer;
 
     void Start () 
     {
         rend = GetComponent<Renderer>();
         childColours = new List<Material>();
         childRenderers = new List<Renderer>();
         childRenderer = GetComponent<Renderer> ();
         StartColour = childRenderer.material;
         for (int i = 0; i < transform.childCount; ++i) {
             childRenderers.Add (transform.GetChild (i).GetComponent<Renderer> ());
             childColours.Add (childRenderers [i].material);
         }
 
     }
 
     void Update () {
 
         if (Input.GetMouseButtonDown (0)) {
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             RaycastHit hit;
 
             if (Physics.Raycast (ray, out hit, 1000)) {
                 ItemCanvas.interactable = true;
                 ItemCanvas.blocksRaycasts = true;
                 ItemCanvas.alpha = 1;
 
                 childRenderer.material = Glow;
                 for (int i = 0; i < childRenderers.Count; ++i)
                     childRenderers [i].material = Glow;
 
 
                 if (hit.collider.tag == "plot_01") {
                     Debug.Log ("plot_01");
                 }
                 if (hit.collider.tag == "plot_02") {
                     Debug.Log ("plot_02");
                 }
                 if (hit.collider.tag == "plot_03") {
                     Debug.Log ("plot_03");
                 }
             }
 
             else{
                 Debug.Log ("you clicked on nothing");
                 ItemCanvas.interactable = false;
                 ItemCanvas.blocksRaycasts = false;
                 ItemCanvas.alpha = 0;
 
                 childRenderer.material = StartColour;
                 for(int i = 0; i < childRenderers.Count; ++i) 
                     childRenderers[i].material = childColours[i];
             }
 
         }
     }
 }
 
 
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 MorphusOne1 · Aug 07, 2017 at 11:23 AM 0
Share

Latest attempt get me the individual plots/gameobjects/blocks to change color/highlight but I can't get them back.

I know I need to store them but it doesn't seem to be working with this method.

No errors or anything.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections.Generic;
 
 public class IsClicked : $$anonymous$$onoBehaviour {
 
     public CanvasGroup ItemCanvas;
     public Renderer rend;
     public $$anonymous$$aterial Glow;
     public GameObject Plot_01;
     public Component[] allChildren;
 
     $$anonymous$$aterial[] Originals;
     $$anonymous$$aterial original$$anonymous$$at;
 
 
     void Start () 
     {
         Plot_01 = GameObject.Find ("Plot_01");
         original$$anonymous$$at = GetComponentInChildren<Renderer>().material;
         }
 
     void Update ()
     {
 
         if (Input.Get$$anonymous$$ouseButtonDown (0)) {
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             RaycastHit hit;
 
             if (Physics.Raycast (ray, out hit, 1000)) {
                 ItemCanvas.interactable = true;
                 ItemCanvas.blocksRaycasts = true;
                 ItemCanvas.alpha = 1;
 
                 if (hit.collider.tag == "plot_01") {
                     Plot_01.GetComponent<Renderer>().material = Glow;
                     Debug.Log ("plot_01");
                 }
                 if (hit.collider.tag == "plot_02") {
                     Debug.Log ("plot_02");
                 }
                 if (hit.collider.tag == "plot_03") {
                     Debug.Log ("plot_03");
                 }
             } else {
                 Debug.Log ("you clicked on nothing");
                 ItemCanvas.interactable = false;
                 ItemCanvas.blocksRaycasts = false;
                 ItemCanvas.alpha = 0;
                 GetComponentInChildren<Renderer>().material = original$$anonymous$$at;
             }
 
         }
     }
 }
avatar image MorphusOne1 · Aug 07, 2017 at 12:48 PM 0
Share

O$$anonymous$$, I've got it working...kinda...

Only one of the plots changes back and forth, the others stay highlighted???

I have this on the prefab -v-

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections.Generic;
 
 public class IsClicked : $$anonymous$$onoBehaviour {
 
     public CanvasGroup ItemCanvas;
     public Renderer rend;
     public $$anonymous$$aterial Glow;
     public Component[] allChildren;
     private GameObject Plot_01;
     private GameObject Plot_02;
     private GameObject Plot_03;
     private GameObject Plot_04;
 
     $$anonymous$$aterial[] Originals;
     $$anonymous$$aterial original$$anonymous$$at;
 
 
     void Start () 
     {
         Plot_01 = GameObject.Find ("Plot_01");
         Plot_02 = GameObject.Find ("Plot_02");
         Plot_03 = GameObject.Find ("Plot_03");
         Plot_04 = GameObject.Find ("Plot_04");
     }
 
     void Update ()
     {
 
         if (Input.Get$$anonymous$$ouseButtonDown (0)) {
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             RaycastHit hit;
 
             if (Physics.Raycast (ray, out hit, 1000)) {
                 ItemCanvas.interactable = true;
                 ItemCanvas.blocksRaycasts = true;
                 ItemCanvas.alpha = 1;
 
                 if (hit.collider.tag == "plot_01") {
                     Plot_01.GetComponent<Renderer>().material = Glow;
                     Debug.Log ("plot_01");
                 }
                 if (hit.collider.tag == "plot_02") {
                     Plot_02.GetComponent<Renderer>().material = Glow;
                     Debug.Log ("plot_02");
                 }
                 if (hit.collider.tag == "plot_03") {
                     Plot_03.GetComponent<Renderer>().material = Glow;
                     Debug.Log ("plot_03");
                 }
 
                 if (hit.collider.tag == "plot_04") {
                     Plot_04.GetComponent<Renderer>().material = Glow;
                     Debug.Log ("plot_04");
                 }
             } 
 
             else 
             {
                 Debug.Log ("you clicked on nothing");
                 ItemCanvas.interactable = false;
                 ItemCanvas.blocksRaycasts = false;
                 ItemCanvas.alpha = 0;
                 GetComponentInChildren<$$anonymous$$aterialSaver> ().Restore ();
             }
 
         }
     }
 }


And then this on each block/child/gameobject -v-

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections.Generic;
 
 public class $$anonymous$$aterialSaver : $$anonymous$$onoBehaviour {
     
     $$anonymous$$aterial[] original$$anonymous$$at;
 
     void Start() {
         
         original$$anonymous$$at = GetComponent<Renderer>().materials;
     }
 
     public void Restore() {
         
         GetComponent<Renderer>().materials = original$$anonymous$$at;
     }
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MorphusOne1 · Aug 08, 2017 at 04:54 AM

Hey Guys,

Well that was quite fun!

I ended up doing it like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections.Generic;
 
 public class IsClicked : MonoBehaviour {
 
     public CanvasGroup ItemCanvas;
     public Renderer rend;
     public Material Glow;
     public Material[] StartColour;
     public Component[] allChildren;
     private GameObject Plot_01;
     private GameObject Plot_02;
     private GameObject Plot_03;
     private GameObject Plot_04;
 
     List<Material> childColours;
     List<Renderer> childRenderers;
     Renderer parentRenderer;
 
     void Start () 
     {
         childColours = new List<Material>();
         childRenderers = new List<Renderer>();
         parentRenderer = GetComponent<Renderer>();
         StartColour = parentRenderer.materials;
         for (int i = 0; i < transform.childCount; ++i)
         {
             childRenderers.Add(transform.GetChild(i).GetComponent<Renderer>());
             childColours.Add(childRenderers[i].material);
         }
         Plot_01 = GameObject.Find ("Plot_01");
         Plot_02 = GameObject.Find ("Plot_02");
         Plot_03 = GameObject.Find ("Plot_03");
         Plot_04 = GameObject.Find ("Plot_04");
     }
 
     void Update ()
     {
 
         if (Input.GetMouseButtonDown (0)) {
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             RaycastHit hit;
 
             if (Physics.Raycast(ray, out hit, 1000)) {
                 ItemCanvas.interactable = true;
                 ItemCanvas.blocksRaycasts = true;
                 ItemCanvas.alpha = 1;
 
                 if (hit.collider.tag == "plot_01") {
                     Plot_01.GetComponent<Renderer>().material = Glow;
                     Debug.Log("plot_01");
                 }
                 if (hit.collider.tag == "plot_02") {
                     Plot_02.GetComponent<Renderer>().material = Glow;
                     Debug.Log("plot_02");
                 }
                 if (hit.collider.tag == "plot_03") {
                     Plot_03.GetComponent<Renderer>().material = Glow;
                     Debug.Log("plot_03");
                 }
 
                 if (hit.collider.tag == "plot_04") {
                     Plot_04.GetComponent<Renderer>().material = Glow;
                     Debug.Log("plot_04");
                 }
             }
 
             else
                 {
                 Debug.Log("you clicked on nothing");
                 ItemCanvas.interactable = false;
                 ItemCanvas.blocksRaycasts = false;
                 ItemCanvas.alpha = 0;
                     parentRenderer.materials = StartColour;
                     for (int i = 0; i < childRenderers.Count; ++i) childRenderers[i].material = childColours[i];
                 }
 
             }
           }
         }

If you can see a better way of doing it please let me know, alternatively if you know how I can apply a quick edit to change all the materials in every slot that would be great! otherwise I'll keep hacking away.

Thanks again!

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 Bill-Sansky · Aug 07, 2017 at 12:34 PM

you are changing the material of each child of your list: don't you want to change it only on the one that was hit by a raycast?

Comment
Add comment · Show 5 · 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 MorphusOne1 · Aug 07, 2017 at 12:50 PM 0
Share

Yes, Only the one that was raycast.

avatar image Bill-Sansky MorphusOne1 · Aug 07, 2017 at 12:54 PM 0
Share

for your update, you want to have your "material saver" on each of your plot, and call them when they are not hit by the raycast anymore

avatar image MorphusOne1 Bill-Sansky · Aug 07, 2017 at 01:00 PM 0
Share

Hi Bill,

Yea, I have. Only Plot_03 behaves correctly?

All the others wont return to the original material.

EDIT: Video of the Issue

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

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

Related Questions

Array of Arrays of GameObjects/Prefabs (C#) 1 Answer

Having multiple objects fire prefabs in different times C# 0 Answers

Grid Placement In Game 2 Answers

How to spawn prefab only in allowed area? 0 Answers

Change Transparency 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