Hi im trying to make a object become outlined after it is clicked, but im having trouble getting the materals array in the mesh renderer, code attached below, thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEditor.SearchService;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UIElements;
public class Click : MonoBehaviour
{
private int intnum;
GameObject[] ClickableObject;
Material[] outline;
Component[] iMat;
void Start()
{
ClickableObject = GameObject.FindGameObjectsWithTag("Clickable Objects");
foreach (GameObject i in ClickableObject)
{
intnum=0;
iMat=i.GetComponents<MeshRenderer>();
}
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
}
}
public void Unclicked()
{
foreach (Material e in iMat)
{
if (iMat[intnum].name == "Outline Material")
{
intnum++;
}
else
{
intnum++;
}
}
foreach (GameObject i in ClickableObject)
{
}
}
}
You have a lot of issues with your code. Honestly, it doesn't make any sense. First, It doesn't compile: "Material e in iMat". This just keeps overwriting the array on each loop: iMat=i.GetComponents<MeshRenderer>(); You might want to use List<MeshRenderer> and use AddRange to the list in each loop. You're getting MeshRenderers but then attempting to store it iMat. iMat contains MeshRenderers, not materials. Materials are part of a MeshRenderer using meshRenderer.materials
– ArachnidAnimalThanks i know my code has a lot of issues as i only started a few weeks ago, and iMat was orignally used to store materals hence the name but i couldn't make it work.
– TrussmisterTo make it a bit more simple you could use this asset here for outlines (https://assetstore.unity.com/packages/tools/particles-effects/quick-outline-115488), all you do is put the outline script on the object you want the outline on and then just enable/disable the script for on/off, might be easier for you than changing the materials themselves. Let me know if this works for your situation and ill submit it as answer rather than a comment :)
– SyntaxSnafuthanks so much!
– Trussmister