Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Alkan · Oct 17, 2011 at 10:35 AM · instantiateshadows

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 :(

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

2 Replies

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

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.

Comment
Add comment · Show 4 · 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
avatar image Alkan · Oct 17, 2011 at 03:30 PM 0
Share

thanks for the response, this is pretty complex for me to grasp unfortunately.

avatar image save · Oct 17, 2011 at 03:38 PM 0
Share

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.

avatar image Alkan · Oct 18, 2011 at 10:06 PM 0
Share

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.

avatar image save · Oct 18, 2011 at 11:00 PM 0
Share

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 ;-)

avatar image
0

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.

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

3 People are following this question.

avatar image avatar image avatar image

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

Detecting what parts of the terrain was hit. 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