How to disable a script inside of a clone?
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;
}
}
}
I also just realized even after I place something down and the obstacle is enabled again, it still dosent have collisons. I've tried adding colliders but that makes it spazz out A LOT . Even when im placing an object down and the obstacle is disabled it has collisions which it shouldn't have, its like the opposite of what it used to be.
Your answer
Follow this Question
Related Questions
Collision or Triggers both not working. 3D. 1 Answer
What is the different between OnTriggerEnter and OnCollisionEnter ? 2 Answers
Preventing A Teleporting GameObject From Passing Through Walls 2 Answers
Sphere is not using OnCollisionEnter/OnTriggerEnter/OnCollisionStay/OnTriggerStay functions 0 Answers