- Home /
Instances of one prefab work wrong together,OnMouseOver works wrong with instances of prefabs
I am starting to make a tower defense and I need to show the range of a specific tower. To do that I have a gameobject, as a child for my tower, with a sprite, showing the range. For more comfortable use I only want to show the sprite renderer when the mouse is on the tower. For that reason I have a special collider. The code is here using System.Collections; using System.Collections.Generic; using UnityEngine; public class appear : MonoBehaviour { SpriteRenderer range; // Start is called before the first frame update private void Start() { range = this.GetComponent<SpriteRenderer>(); } private void OnMouseOver() { range.enabled = true; } private void OnMouseExit() { range.enabled = false; } }
Apparently when there are more than one instance of the tower prefab, some towers don’t even show the range and some only work on a part of a collider,I am making a tower defense game and my towers are made out of two game objects, one the tower itself and the other- an empty gameobject with a sprite renderer turned off. I have a simple script to detect when to show the sprite on the empty gameObject, but for some reason multiple instances of this prefab don’t work together. Here is the code for the sprite renderer
using System.Collections; using System.Collections.Generic; using UnityEngine; public class appear : MonoBehaviour { SpriteRenderer range; // Start is called before the first frame update private void Start() { range = this.GetComponent<SpriteRenderer>(); } private void OnMouseOver() { range.enabled = true; } private void OnMouseExit() { range.enabled = false; } }
Your answer
Follow this Question
Related Questions
How to scroll progressively a game object? 0 Answers
My gameObject disappear after using mouse's position 2 Answers
Distribute terrain in zones 3 Answers
Make a gameobject when you click 2 Answers