Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Chocolade · Apr 05, 2017 at 09:30 PM · c#scripting problemscript.

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.

Screenshot

ss109.jpg (78.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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>();
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

313 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges