- Home /
Turn off script of object clones
I have a building system in my game and I made it so whenever its being moved the colliders are turned off so you cant push everything around, then when its placed it turns back on. It works fine but the problem is all of the clones have that same script so whenever I'm placing down a new object all of the previous placed objects will turn off colliders as well. Is it possible to detect clones of an object and then run code to turn the script off as they're placed down and dont need to be accessed anymore? I hope I explained that well enough. Heres my placing script `
using UnityEngine;
using UnityEngine.AI;
public class GroundPlacementController : MonoBehaviour
{
[SerializeField]
private GameObject placeableObjectPrefab;
public NavMeshObstacle nav;
[SerializeField]
private KeyCode newObjectHotkey = KeyCode.A;
private GameObject currentPlaceableObject;
private float mouseWheelRotation;
private void Update()
{
HandleNewObjectHotkey();
nav = GetComponent<NavMeshObstacle>();
if (currentPlaceableObject != null)
{
MoveCurrentObjectToMouse();
RotateFromMouseWheel();
ReleaseIfClicked();
}
}
private void HandleNewObjectHotkey()
{
if (Input.GetKeyDown(newObjectHotkey))
{
if (currentPlaceableObject != null)
{
Destroy(currentPlaceableObject);
}
else
{
currentPlaceableObject = Instantiate(placeableObjectPrefab);
}
}
}
private void MoveCurrentObjectToMouse()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
currentPlaceableObject.transform.position = hitInfo.point;
currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
nav.enabled = false;
}
}
private void RotateFromMouseWheel()
{
Debug.Log(Input.mouseScrollDelta);
mouseWheelRotation += Input.mouseScrollDelta.y;
currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * 90f);
}
private void ReleaseIfClicked()
{
if (Input.GetMouseButtonDown(0))
{
currentPlaceableObject = null;
nav.enabled = true;
}
}
}
Answer by CybexGS · Mar 14, 2019 at 02:47 PM
You need to turn off and on the 'NavMeshObstacle' component which is attached to your 'currentPlacableObject'.
Instead of:
nav.enabled = false;
do
currentPlaceableObject.GetComponent<NavMeshObstacle>().enabled = false;
Same for activating it again, but = true;
of course.
@CybexGS I dont think that will work because on line 82 it says currentPlaceableObject = null
so whenever it calls to get the component it cant find it.
Edit: I switched the lines around and now it works fine
Answer by Deathdefy · Mar 13, 2019 at 03:27 PM
You are universally looking for and handling mouse input on every single object every frame. You need to only handle it when you are clicking on the actual object you want to do something to.
Look into using the OnMouseDown method. It will only execute on the object that is currently being clicked down on instead of just checking universally if the mouse button is down.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
It only calls On$$anonymous$$ouseDown whenever the mouse is over the collider. $$anonymous$$y mouse isnt on the actual object most of the time so it dosnt work and only places it down as soon as I press 0 (key to toggle build mode). I also just realized every time I place down another one the amount of clones it makes increases by one. For example when I place down the first clone it only makes one of them, when I place down the second though 2 clones are in the same position, 3rd time has 3 and so on.