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 Michel Verheughe · Aug 08, 2013 at 09:28 PM · children

Script sharing

I'll have to ask this again because I still don't understand:

I have a game object with a script attached. That game object has several children objects. The script reads variables entered from the Inspector.

Then I make duplicates of that object, each with different variables affecting the children objects. It doesn't work! The children follow the variable settings of the first duplicated object in the Hierarchy, not their respective ones.

My codes are something like this:

 var thisShip = GameObject.Find("Ship");
 thisShip.transform.rotation.eulerAngles.y = heading;

But all children objects from the different objects with attached script show the same "heading." What went wrong?

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

Answer by robertbu · Aug 08, 2013 at 09:36 PM

The problem is that GameObject.Find() searches the entire scene and returns the first game object with that name. Assuming your code is trying to assign the parent to the 'thisShip' variable, you can do:

 var thisShip = transform.parent.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
avatar image
0

Answer by Michel Verheughe · Aug 09, 2013 at 08:22 AM

I was so exited when I got up this morning (Norwegian time) reading your answer because it makes sense to add "parent." But ... I can't get it to work. ;-(

To test, I have only two ships, both sharing a script that is the model of the ship. They have different parameters such as initial speed, etc. entered from the Inspector window. This is because I will have to make many exercises, each with a different ship configuration.

Then, as children of the ship object, I have other objects with their own scripts. To get this to work, the children objects and their scripts must read the initial parameters of the parent ship object in the parent script. For example, in the script of a child object, I write:

 var thisShip = transform.parent.gameObject; //the parent ship object
 var thisModel = thisShip.GetComponent(shipModel); //the parent ship script
 Debug.Log(thisModel.theName); //this should show the name of the ship

It only shows the name of one ship, probably the first one in the scene. What went wrong? Tanks in advance.

Comment
Add comment · Show 2 · 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 Michel Verheughe · Aug 09, 2013 at 11:16 AM 0
Share

Even when I write:

 var thisShip: GameObject;


and I enter the name of the ship object in the Inspector, when I then write:

 var model = thisShip.GetComponent(ship$$anonymous$$odel);

and read a variable like:

 Debug.Log(model.isName);


it shows, for both ships, the same name; not the different names of the two ships that are as the variables isName

I must say that I don't understand anything! ;-(

avatar image Michel Verheughe · Aug 11, 2013 at 06:40 AM 0
Share

When making several copies of an object with a script, and children with scripts, yet having different parameters, here is how I found out it could work:

 var parentObject = transform.parent.gameObject;
 var thisChildObject = GameObject.Find(parentObject.name + "/child/grandChild");

Or, if the script is in the parent object:

 var thisChildObject = GameObject.Find(this.name + "/child/grandChild");

Not the easy way around but it works. Perhaps it can help others.

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

14 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Activate children 2 Answers

FInd the min and max position of children? 1 Answer

finding a child object by string 1 Answer

Changing the parent's position based on a child's variable 0 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