44108

how to clone several game objects in a way that clone properties of one can be adjusted to match all

I asked How can I adjust shape/dimensions of one clone to affect all other clones in the scene view and the accepted answer was spot on. It could only clone one game object. I tried making some adjustments but the only solution I came up with was adding duplicate methods for additional objects. This doesn't work well when dealing with several game objects to be cloned. How can I clone several game objects in a similar way so that adjusting the components and properties live of one clone is replicated on all the others scene view without having to write duplicate methods.

Answer1:

You should use events. Unity3d tutorials has a good, simple explanation: https://unity3d.com/learn/tutorials/topics/scripting/events

Answer2:

It sounds like you have an object that has several clones. You want changing the shape or dimensions of any of those objects to affect the other ones?

For this to happen, each object needs to know about the other ones. You can do this decentralized (each object contains a reference to each other) or centralized (one object governs the rest).

The centralized approach is more simple so I'll give a simple example.

public class Shape { public int length; } public class ShapeCentral { public List<Shape> shapes = new List<Shape>(); public void CloneShape() { //instantiate new shape shapes.Add(new Shape()); } public void SetCloneLength(int l) { shapes.ForEach(x => x.length = l); } }

As you can see, one object can control all the clones at once. The trick is to not create clones using other methods or you will run into trouble.

If you want to tighten up your variable access (which I recommend, its a good exercise) you could use a publisher/subscriber pattern. In this, when a new clone is instantiated, it subscribes to the SetCloneLength method. When you want to change the length, the central class publishes that message and it is sent to all the subscribers.

The difference here is that in my example, the central class needs to keep track of all the clones, in publisher/subscriber, you don't.

Recommend

  • What is a NavMesh in Unity?
  • Unity events in animation missing
  • Replacing array key spaces with underscores in multidimensional array
  • Excel vba error with dates on sheet and textbox
  • build-impl.xml:1025: The module has not been deployed. (Wicket)
  • Generating smoothed randoms that follow a distribution
  • Rendering complete camera view(16:9) onto a texture in Unity3d
  • Different version of file in Mercurial repo
  • Django Rest Framework - Best way to deal with API parameter validation errors?
  • How to connect to a GitHub repo using libgit2?
  • Regarding Static class in c# [duplicate]
  • C# COM Component Fails To Read Config When Loaded Into An Unmanaged C++ App
  • Bootstrap Popover showing at wrong place upon zoom in/out or resizing Browser window
  • Converting simple MySQL database to a NoSQL solution
  • how to get data attributes of dynamically generated element
  • apply a javascript function to draggable copy
  • Compare struct to a constant in C
  • Can someone please explain to me in the most layman terms how to use EventArgs?
  • Position: fixed nav does not stay fixed
  • gspread or such: help me get cell coordinates (not value)
  • how to avoid repetitive constructor in children
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • how to display data from 1st point on words on y axis for line chart in d3.js
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Row Count Is Returning the incorrect number using RaptureXML
  • Typescript - Unable to get 'import' statement to function
  • Spark fat jar to run multiple versions on YARN
  • Q promise. Difference between .when and .then
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • How to add date and time under each post in guestbook in google app engine
  • Updating server-side rendering client-side
  • WinForms: two way TextBox problem
  • ActionScript 2 vs ActionScript 3 performance
  • json Serialization in asp
  • C# - Getting references of reference
  • coudnt use logback because of log4j
  • git trying to push non-existent file … after clearing cache