- Home /
How can i set the same script on two GameObjects so the script will work on both objects same time ?
For now if i add the script to both gameobjects it will work only on one of them. Once removed the script from the first gameobject it will work on the second one the script is attached to. But i want it to work on both gameobjects.
The script:
using System;
using UnityEngine;
using Random = UnityEngine.Random;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class CloneObjects : MonoBehaviour
{
public GameObject ObjectToCreate;
public int objectsHeight = 3;
[HideInInspector]
public GameObject[] objects;
// for tracking properties change
private Vector3 _extents;
private int _objectCount;
private float _objectSize;
/// <summary>
/// How far to place spheres randomly.
/// </summary>
public Vector3 Extents;
/// <summary>
/// How many spheres wanted.
/// </summary>
public int ObjectCount;
public float ObjectSize;
public static float LargestSize = 0;
// Use this for initialization
void Awake()
{
Clone();
objects = GameObject.FindGameObjectsWithTag("ClonedObject");
foreach (var element in objects)
{
float Size = element.transform.localScale.x;
if (Size > LargestSize)
LargestSize = Size;
}
}
private void OnValidate()
{
// prevent wrong values to be entered
Extents = new Vector3(Mathf.Max(0.0f, Extents.x), Mathf.Max(0.0f, Extents.y), Mathf.Max(0.0f, Extents.z));
ObjectCount = Mathf.Max(0, ObjectCount);
ObjectSize = Mathf.Max(0.0f, ObjectSize);
}
private void Reset()
{
Extents = new Vector3(250.0f, 20.0f, 250.0f);
ObjectCount = 100;
ObjectSize = 20.0f;
}
// Update is called once per frame
void Update()
{
}
private void Clone()
{
if (Extents == _extents && ObjectCount == _objectCount && Mathf.Approximately(ObjectSize, _objectSize))
return;
// cleanup
var ObjectsToDestroy = GameObject.FindGameObjectsWithTag("ClonedObject");
foreach (var t in ObjectsToDestroy)
{
if (Application.isEditor)
{
DestroyImmediate(t);
}
else
{
Destroy(t);
}
}
var withTag = GameObject.FindWithTag("Terrain");
if (withTag == null)
throw new InvalidOperationException("Terrain not found");
for (var i = 0; i < ObjectCount; i++)
{
var o = Instantiate(ObjectToCreate);
o.tag = "ClonedObject";
o.transform.SetParent(base.gameObject.transform);
o.transform.localScale = new Vector3(ObjectSize, ObjectSize, ObjectSize);
// get random position
var x = Random.Range(-Extents.x, Extents.x);
var y = Extents.y; // sphere altitude relative to terrain below
var z = Random.Range(-Extents.z, Extents.z);
// now send a ray down terrain to adjust Y according terrain below
var height = 10000.0f; // should be higher than highest terrain altitude
var origin = new Vector3(x, height, z);
var ray = new Ray(origin, Vector3.down);
RaycastHit hit;
var maxDistance = 20000.0f;
//var nameToLayer = LayerMask.NameToLayer("Terrain");
var nameToLayer = LayerMask.GetMask("Terrain");
var layerMask = 1 << nameToLayer;
if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
{
var distance = hit.distance;
y = height - distance + y; // adjust
}
else
{
Debug.LogWarning("Terrain not hit, using default height !");
}
// place !
o.transform.position = new Vector3(x, y + objectsHeight, z);
}
_extents = Extents;
_objectCount = ObjectCount;
_objectSize = ObjectSize;
}
}
In the screenshot on the right it's the inspector of the WayPoints i clone some gameobjects. On the bottom it's the inspector of the SpaceShips there i clone other gameobject. But only one of them will work. If they both set with the script only the WayPoints will work, but i want them both to work the WayPoints and the SpaceShips work i mean toe clone the different objects in each gameobject i attached the script and dragged a different gameobject(prefab) to each script in the inspector.
Answer by UnityCoach · Apr 05, 2017 at 10:43 PM
That script is really designed to work on one object only. You can either edit it so that several instances of the script can work together, or edit it so that it can work with several objects with one instance of the script. The main issue is that it uses object tags to find objects in the scene. You can simply add objects to a list instead.
o.tag = "ClonedObject";
// replace with
cloneList.Add(o);
and in other places
objects = GameObject.FindGameObjectsWithTag("ClonedObject");
// replace with
objects = cloneList.ToArray();
for this to work, you need an object list,
List<GameObject> cloneList = new List<GameObject>();
Your answer
Follow this Question
Related Questions
Why when creating new animator controller for the character the character is not walking right ? 0 Answers
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers
How can i spawn new gameobjects to be inside the terrain area ? 2 Answers