Is there a way that the player can contantly be checking for multiple gameObjects with a specific tag and then disable a script from each gameObject that gets close? I need some way for the player to be able to send a message to that speicific gameObject to disable the script attached. Is this possible?
2 Answers
2Here is my “average” answer - which I can provide with the information given (because this is a Knowledge Base and others may well search for this). Here are a couple of points:
- SphereColliders would be faster and clearer
- Just moving the script from the 100s of objects to the player (as asserted in this question and its duplicate) would actually make it slower not faster
- The probable best solution to this is to trade some memory for a fast lookup to do rough range checks first
#RangeCheckObject.cs
using UnityEngine;
using System.Collections.Generic;
public class RangeCheckObject : MonoBehaviour
{
public static HashSet<Transform> allRangeCheckObjects = new HashSet<Transform>();
void OnEnable()
{
allRangeCheckObjects.Add(transform);
}
void OnDisable()
{
allRangeCheckObjects.Remove(transform);
}
}
#InRangeCheck.cs
using UnityEngine;
using System.Collections;
public class InRangeCheck : MonoBehaviour
{
public float frequency = 0.1f; //Set to 0 for every frame
public float distance = 1f;
IEnumerator Start()
{
yield return new WaitForSeconds(UnityEngine.Random.value*frequency);
while(true)
{
var position = transform.position;
var distanceSquared = distance * distance;
foreach(var item in RangeCheckObject.allRangeCheckObjects)
{
if((item.position - position).sqrMagnitude < distanceSquared)
item.SendMessage("InRange", this, SendMessageOptions.DontRequireReceiver);
}
yield return new WaitForSeconds(frequency);
}
}
}
#ScriptRemover.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ScriptRemover : MonoBehaviour
{
public List<string> scriptsToRemove;
public List<Component> componentsToRemove;
public bool removeRangeCheck = true;
void InRange() //Can optionally specify to know which range checker to use.
{
foreach(var script in scriptsToRemove)
{
var o = GetComponent(script);
if(o) Destroy(o);
}
foreach(var comp in componentsToRemove)
{
if(comp) Destroy(comp);
}
if(removeRangeCheck)
{
var o = GetComponent<RangeCheckObject>();
if(o) Destroy(o);
}
componentsToRemove.Clear();
scriptsToRemove.Clear();
}
}
This tries to balance the effort of checking out over a few frames if there is more than one thing doing the checking, uses the most optimal form of distance check and the most optimal way of maintaining a list of the target objects.
It could be improved by adding a tag and a dictionary rather than just a HashSet to the checking, so that different types of object could be detected.
You attach a RangeCheckObject and a ScriptRemover to the target object. In the ScriptRemover you can specify script names as text or as a dragged reference in the inspector. It optionally allows you to remove the RangeCheckObject to ensure it isn’t called again.
Thank you for putting a lot of effort into writing all this! SendMessage is what I was looking for. Seems to make it simple to find surrounding objects and then activate the script that I needed it too.
– jessee03I dont see any reason why you cant use a sphere collider as a trigger. I assume its because your worried about other gameobjects being triggered or something like that. if this is the problem you could create a collision layer for these objects. The way you want to do it could slow down your program if you had to many objects.
What do you mean by close ? Close geographically to the player ?
– KiraSenseiwithin a certain distance from the player.
– jessee03It's complex to explain but I can't use triggers for what I'm doing. I just need to check distance and disable a script from that close gameObject.
– jessee03Right now I have hundreds of these objects in the scene and each one has a script that is constantly checking when the player is close. Once the player is close enough it disables the script attached to the object. The problem is there are too many scripts being called constantly. Making it so that only the player needs to check when these objects are in range would make it so I would get much better fps. Problem is I don't know how to access a certain tagged object in range / disable the script attached to it.
– jessee03Because it needs to be from the center of the objects and how I have it setup triggers won't work to well or that would of been my first choice, I need to calculate distance. I really just need a simple answer from anyone if you are able to check for any objects with a certain tag that are within a certain distance from the player and then are able to disable the script on said object. I feel like people are avoiding the question at hand.
– jessee03