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 redteardrop · Aug 07, 2012 at 04:25 AM · instantiateprefabvariablechild

Passing a variable to the child of Instantiated object?

I'm working on a destructible environment and when a wall breaks 4 equal parts fall away. In script I want to pass a variable to each of the 4 child instances. I wouldn't think this would work properly if the prefab of the 4 parts were in a group when the script with the variable is attached to each wall piece not the actual prefab.

I was looking at something like this example I found in the questions area.

    Bullet zBullet = Instantiate(bullet, transform.position, Quaternion.identity) as Bullet;
zBullet.playerNum = playerNumber;

But I think this would just make the zBullet have the variable not any child of it.

Any way to pass a variable to a child of an Instantiated prefab? thanks

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
0
Best Answer

Answer by aldonaletto · Aug 07, 2012 at 04:36 AM

If the four parts have the same script (let's suppose it's called ChildScript.cs) you can do something like this:

public GameObject wallPrefab; // wall prefab
  ...
  // Instantiate the wall:
  GameObject wall = Instantiate(wallPrefab, ...) as GameObject;
  // for all children in wall...
  foreach (Transform child in wall.transform){
    // try to get the child script:
    ChildScript script = child.GetComponent< ChildScript>();
    if (script){ // if it has such script...
      script.someVar = someValue; // set its variable
    }
  }
  ...

EDITED: I thought your code was in C# - there goes the JS version:

var wallPrefab: GameObject; // wall prefab
  ...
  // Instantiate the wall:
  var wall: GameObject = Instantiate(wallPrefab, ...);
  // for all children in wall...
  for (var child: Transform in wall.transform){
    // try to get the child script:
    var script: ChildScript = child.GetComponent(ChildScript);
    if (script){ // if it has such script...
      script.someVar = someValue; // set its variable
    }
  }
  ...
Comment
Add comment · Show 3 · 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 redteardrop · Aug 08, 2012 at 02:42 PM 0
Share

Doesn't seem to work for me. GameObject wall = Instantiate tells me I need a ; in the line, but using var wall = Instantiate works.


my main issue though is that whenever I try passing a variable to the child or even the prefab itself the engine freezes and it looks like it's trying to Instantiate infinite wall pieces.


function OnCollisionEnter(collision : Collision)
{if(health < 0)
    {
        var temp:Quaternion = transform.rotation;
        var wall = Instantiate(blox, transform.position, temp);
        wall.health=100;
        Destroy (gameObject);
    }
}
the wall.health=100; is where I get the trouble of it crashing. I can remove that line and all works out ok. I can put anything there with "wall." and it seems to crash. a transform.rotation seems to not crash it, but a transform.scale does. I can't get my head around what the issue could be.

avatar image redteardrop · Aug 11, 2012 at 03:31 PM 0
Share

I need it in JS, that looks like C. Would you know how to convert it to js? $$anonymous$$ainly the foreach section. I'm pretty sure I can do the rest.

avatar image redteardrop · Oct 04, 2012 at 03:40 PM 0
Share

I got it to work, but I don't think I am using a parent child relationship anymore. here's what I did:

var oneBlox : GameObject;
oneBlox = Instantiate (targetBlock, tempPosision, tempRotation);
var bloxScript1 =
oneBlox.GetComponent("Blox"); // name of script without the ".js" at the end.
bloxScript1.blockSize = 2; //edit the blockSize variable in the instantiateed object
bloxScript1.blockType = blockType; //edit the blockType variable in the instantiateed object

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

8 People are following this question.

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

Related Questions

Any ideas on a simple AI script that doesn't use any drag/drop variables 1 Answer

[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers

Spawn Shield to all objects 2 Answers

Instantianig a teleporter prefabs with destination variables 1 Answer

Give prefab a parent 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