1

I have an object with a script (Defender) attached to it. This script has a defRadius property. Also I am spawning my object in a circle using the following code:

CircleSpawn

public class CircleSpawn : MonoBehaviour
{
    public float radius, radiusLast, spin, spinLast;
    public int numOfItems;
    public GameObject clonedObject;
    public List<GameObject> spawnedObjects;
}

CircleSpawnEditor

[CustomEditor(typeof(CircleSpawn))]
public class CircleSpawnEditor : Editor
{
public override void OnInspectorGUI ()
{
    var tar = (CircleSpawn)target;    
    EditorGUILayout.LabelField("Radius"); // Set as required
    tar.radius = EditorGUILayout.Slider(tar.radius, 0f, 100f);          
    EditorGUILayout.LabelField("Angle"); // Set as required
    tar.spin = EditorGUILayout.Slider(tar.spin, 0f, 360f);              
    EditorGUILayout.LabelField("Number of Items"); // Set as required
    tar.numOfItems = EditorGUILayout.IntSlider(tar.numOfItems, 0, 36);  
    EditorGUILayout.LabelField("Object");
    tar.clonedObject = (GameObject)EditorGUILayout.ObjectField(tar.clonedObject, 
    typeof(GameObject), true);

    float angle, angleBetween = 360.0f / tar.numOfItems;

    if (tar.spawnedObjects == null)
        tar.spawnedObjects = new List<GameObject>();

    // Solution #1    
    if (tar.spawnedObjects.Count != tar.numOfItems)
    {
        foreach (var ob in tar.spawnedObjects)
            DestroyImmediate(ob);

        tar.spawnedObjects.Clear();
        angle = 0f;

        for (int i = 0; i < tar.numOfItems; i++)
        {
            var rot = Quaternion.Euler(0f, 0f, tar.spin + angle);
            var localPos = rot * Vector3.right * tar.radius;    
            tar.spawnedObjects.Add(Instantiate(tar.clonedObject,
            tar.transform.position + localPos, rot));
            angle += angleBetween;
        }
    }

    // Solutions #2 & 3    
    if (!Mathf.Approximately(tar.spin, tar.spinLast) ||
        !Mathf.Approximately(tar.radius, tar.radiusLast))
    {
        tar.spinLast = tar.spin;
        tar.radiusLast = tar.radius;    
        angle = 0f;

        for (int i = 0; i < tar.numOfItems; i++)
        {
            var rot = Quaternion.Euler(0f, 0f, tar.spin + angle);
            var localPos = rot * Vector3.right * tar.radius;    
            tar.spawnedObjects[i].transform.position = 
            tar.transform.position + localPos;
            tar.spawnedObjects[i].transform.rotation = rot;
            angle += angleBetween;
        }
    }
}
}

How can I spawn the objects replacing radius in CircleSpawn with the defRadius (property of the Defender script attached to the object to be cloned)?

Lece
  • 2,339
  • 1
  • 17
  • 21
NSSwift
  • 395
  • 1
  • 12
  • Hey, so you just want to change the value on the original prefab, right? – Lece May 03 '18 at 05:03
  • @Lece yes. Generally changing it's value for the `radius` in `CircleSpawn` – NSSwift May 03 '18 at 05:08
  • Does that answer your question? – Lece May 03 '18 at 09:07
  • @Lece Precisely – NSSwift May 03 '18 at 10:53
  • 1
    Excellent! And if you ever wish to save the other variables (spin/numItems) to the original prefab, simply replicate what I've done with radius :) – Lece May 03 '18 at 11:34
  • @Lece I would be grateful if you could take a look at this question for me https://stackoverflow.com/questions/50167887/how-can-i-adjust-shape-dimensions-of-one-clone-to-affect-all-other-clones-in-the – NSSwift May 04 '18 at 05:41

1 Answers1

2

To read and write values from the original prefab, we first save a reference to the new script Defender and replace tar.radius in the original code:

public class Defender : MonoBehaviour
{
    public float Radius;
}

[CustomEditor(typeof(CircleSpawn))]
public class CircleSpawnEditor : Editor
{
    private Defender _defender;

    public override void OnInspectorGUI ()
    {
        var tar = (CircleSpawn)target;
        tar.clonedObject = (GameObject)EditorGUILayout.ObjectField(tar.clonedObject,
            typeof(GameObject), true);
        if (!tar.clonedObject) return;

        if (!_defender)
        {
            _defender = tar.clonedObject.GetComponent<Defender>();
            if (!_defender) return;
        }

        EditorGUILayout.LabelField("Radius"); // Set as required
        _defender.Radius = EditorGUILayout.Slider(_defender.Radius, 0f, 100f);
        EditorGUILayout.LabelField("Angle"); // Set as required
        tar.spin = EditorGUILayout.Slider(tar.spin, 0f, 360f);
        EditorGUILayout.LabelField("Number of Items"); // Set as required
        tar.numOfItems = EditorGUILayout.IntSlider(tar.numOfItems, 0, 36);
        EditorGUILayout.LabelField("Object");

        float angle, angleBetween = 360.0f / tar.numOfItems;

        if (tar.spawnedObjects == null)
            tar.spawnedObjects = new List<GameObject>();

        // Solution #1
        if (tar.spawnedObjects.Count != tar.numOfItems)
        {
            foreach (var ob in tar.spawnedObjects)
                DestroyImmediate(ob);

            tar.spawnedObjects.Clear();
            angle = 0f;

            for (int i = 0; i < tar.numOfItems; i++)
            {
                var rot = Quaternion.Euler(0f, 0f, tar.spin + angle);
                var localPos = rot * Vector3.right * _defender.Radius;
                tar.spawnedObjects.Add(Instantiate(tar.clonedObject,
                tar.transform.position + localPos, rot));
                angle += angleBetween;
            }
        }

        // Solutions #2 & 3
        if (!Mathf.Approximately(tar.spin, tar.spinLast) ||
            !Mathf.Approximately(_defender.Radius, tar.radiusLast))
        {
            tar.spinLast = tar.spin;
            tar.radiusLast = _defender.Radius;
            angle = 0f;

            for (int i = 0; i < tar.numOfItems; i++)
            {
                var rot = Quaternion.Euler(0f, 0f, tar.spin + angle);
                var localPos = rot * Vector3.right * _defender.Radius;
                tar.spawnedObjects[i].transform.position =
                tar.transform.position + localPos;
                tar.spawnedObjects[i].transform.rotation = rot;
                angle += angleBetween;
            }
        }
    }
}
Lece
  • 2,339
  • 1
  • 17
  • 21