Question by
CraftyMaelyss · Feb 12, 2020 at 07:00 AM ·
c#skinnedmeshrendererenable and disable script
How to activate Mesh Renderers on Trigger Collission
Heya!
So previously I wanted to activate every item that had the tag apparel, but this time I want to enable a Mesh Renderer when the player collides with a collider.
Below is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ApparelActivate : MonoBehaviour
{
public GameObject Apparel;
public SkinnedMeshRenderer skinnedMeshRenderer;
// Use this for initialization
void Start()
{
SkinnedMeshRenderer m = GetComponent<SkinnedMeshRenderer>();
m.enabled = false;
}
void Update()
{
SkinnedMeshRenderer m = GetComponent<SkinnedMeshRenderer>();
m.enabled = true;
}
void OnTriggerEnter(Collider collider)
{
if (collider.tag == "Player")
{
//skinnedMeshRenderer.active = true;
}
}
}
When I have the Mesh Renderers for each apparel/item/gameobject unchecked, as soon as I start the game, it enables the Mesh Renderers, so I know that function is working. I'm just a bit stuck as to why it's enabling immediately.
Thanks in advance!
Comment
Best Answer
Answer by CraftyMaelyss · Feb 12, 2020 at 09:06 AM
This took a lot of experimentation, since for some reason, regular methods of activating objects would not work. I'll put a copy of my script here in case it helps anybody out!
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ApparelActivate : MonoBehaviour
{
[SerializeField] public GameObject gameObject;
void OnTriggerEnter (Collider other)
{
if(other.CompareTag("Player"))
{
gameObject.active = true;
Debug.Log("Received items.");
}
}
}
Hopefully you won't have to go through the headache that I did!