- Home /
Modifying scripts during run-time
Is it possible to modify details of a script while the game is running?
I need a projector shadow to instantiate and follow prefabs as and when they are instantiated.
The first prefab that is created is given the gameobject name "cube0", when this is instantiated a projector shadow is also created and told to follow cube0 with the following code:
transform.position = gameObject.Find("cube0").transform.position;
As I continue to instantiate new prefabs (cube1, cube 2 etc) I need to instantiate more instances of the same shadow projector but make sure they each point towards the correct game object (ie follow cube1, cube 2 etc)
but I'm not quite sure how to automate this in code, I thought about making the "object to follow" a variable and changing this every time a new prefab is instantiated, but I don't think this is going to work as all the existing shadows already in the scene will start to follow the wrong object.
I get the feeling the solution to this is simple, but my brain is failing me this morning :(
Answer by save · Oct 17, 2011 at 10:45 AM
Try to child it instead at instantiation. Never use the find-function more than once for an object that should remain the same (doing it inside Update literally eats juice).
var theBlob : GameObject; var cubePrefab : GameObject; private var cube : GameObject; private var cubeNumber : int = 0;
function Start () { cube = GameObject.Find("cube"+cubeNumber.ToString()); }
function Spawn () { cube = GameObject.Find("cube"+cubeNumber.ToString()); //or cube = Instantiate(cubePrefab, thePosition, theRotation); cube.name = "cube"+cubeNumber.ToString();
thisBlob = Instantiate(theBlob, cube.transform.position, Quaternion.identity);
thisBlob.transform.parent = cube.transform;
cubeNumber++;
}
This way you won't have lots of transform.position
setting and could have lots of cubes with blob shadows.
thanks for the response, this is pretty complex for me to grasp unfortunately.
No worries, just ask away!
The idea is that you do everything once at instantiation, then let parenting do the rest. You can call the Spawn-function whenever you want to instantiate a new cube, inside the same function instantiate the projector for that object.
Referencing to an instantiated object is done by giving it a variable: var cube = Instantiate(..
Then you can instantiate another object and work towards the earlier reference:
var cube : GameObject = Instantiate(cubePrefab, Vector3.zero, Quaternion.identity);
var blob : GameObject = Instantiate(blobPrefab, Vector3.zero, Quaternion.identity);
blob.transform.parent = cube.transform;
What happens in that code is that we set cube and blob as variables while we spawn them, then set the blob as a child to the cube.
You probably want a static function
so you can call Spawn() from wherever you want.
If every cube should have a projector as standard then you could as well child it directly and make a prefab out of it, which would make things easier.
Thanks for trying to help!
Ok, so I've looked through your example - and while I haven't tried it out (yet) it does make sense.
Does blob.transform.parent = cube.transform; only copy the blobs position and NOT it's rotation to the cube?
The problem I've got is as follows - currently I spawn each cube with the same variable name within an if statement - here is my code:
if (Input.GetButtonDown("Shoot")&&i
shootdelay();
var instance : GameObject = Instantiate(currentlevel[i],transform.position, transform.rotation);
i++;
The prefab that is instantiated each time changes with the variable "i" but the actual variable instance "instance" that is used for instantiation remains the same.. so I'm not sure how I would individually parent them to blob shadows.
I'm not entirely sure if I get what you're developing, does every cube have a projector?
When you child something it will inherit the world position and rotation of the parent. To modify the child use the local position or rotation. To lock rotation of an object you can either use a rigidbody set to kinematic with rotation axes locked or script something like transform.rotation = Quaternion.Euler(0, 0, 0);
or in LateUpdate()
set transform.rotation = initialRotation (where var initialRotation : Quaternion = transform.rotation
is set from Awake()
or Start()
).
If you need to keep track of objects further on in a scene use arrays as reference to them. When you instantiate one cube you also add a blob to an array. Then you'd be able to call blobArray[int].transform.position = cubeArray[int].transform.position
for instance. To reference even better use a variable on each object which is their ID in their corresponding arraylist. Then you could look up the objects ID and make changes to the array at the same position in the array as their variable number.
Also don't underestimate Unity's power of iterating through for-loops ;-)
Answer by Alkan · Oct 19, 2011 at 12:39 PM
Every cube (or instantiated prefab) needs it's own projector shadow yes - originally I actually included the shadows as a child of each prefab and then attempted to use a script on the shadow which forced it look downwards in world space despite what the parent prefab was doing (the prefabs are flung through the air with rotation) but I could never get this to work reliably, behaviour seemed buggy and as the parent rotated the projector would often become misaligned or offset from the parents centre of origin slightly. If I could find a way of making this method work I would prefer it over other solutions as I like the simplicity of shadows being grouped with objects without the need for scripting.
Your idea of matching shadow arrays with an object array seems appealing as I already have a list of instantiated prefabs stored in an array (if you can imagine the player is given access to each prefab one after another almost like the way blocks are received in a queue when playing Tetris) so I guess I could look into that.
Your answer
![](https://koobas.hobune.stream/wayback/20220613012358im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
prefab instantiated by script containing ShadowCaster2D bugs out 0 Answers
Shadow & Reflection in First Person? 0 Answers
Spawn from array 2 Answers