Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by mahbubndc · Aug 24, 2015 at 01:34 PM · instantiateassign

assigning gameobject to instantiated gameobjects script

I have instantiated an object in a scene and now I need to assign another object in a script of that instantiated object ..How to do this?

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
2

Answer by christuart · Aug 24, 2015 at 02:42 PM

First you need to find that other script, using GetComponent(). Then you need to access the public GameObject variable which you must have in your script, setting it to the GameObject you want to assign.

Say your script within the instantiated object is called "DoAThing" and within it you have a variable:

 public class DoAThing : MonoBehaviour {
 
     public GameObject target;

    // etc.
     void Start() {
     }
     //...
 }

Now within your script that instantiates an object holding the DoAThing script, you will have written

 GameObject instantiatedObject = Instantiate(myPrefab) as GameObject;

or similar. Now add to this:

 DoAThing myScript = instantiatedObject.GetComponent<DoAThing>();
 myScript.target = myTargetObject;

where myTargetObject is the GameObject you need applied to the script.

(If you won't ever need to change anything else about your script, you could shorten this to

 instantiatedObject.GetComponent<DoAThing>().target = myTargetObject;
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 Landern · Aug 24, 2015 at 01:52 PM

Well, since there aren't any examples, lets assume there is some script that gets it's typical Unity Behaviour methods called, like Awake and Start.

If it's not important to do it during Awake and could be called each time this gameobject is enabled/disabled then start would be the best place. You need to know the script names that you want to adjust or change values on that are attached to this newly instantiated gameobject.

 // gameobject base script
 using UnityEngine;
 using System.Collections;
 
 public class MyGameObjectScript: MonoBehaviour 
 {
     void Start() 
     {
         // first find the script using GetComponent
         MyOtherScriptType myscript = GetComponent<MyOtherScriptType>();
         // with a reference, set your variable, this must be accessible and not private
         myscript.MyGameObject = transform; // pretend the MyGameObject is of type Transform
     }
 }

 // The other attached script
 
 using UnityEngine;
 using System.Collections;
 
 public class MyOtherScriptType : MonoBehaviour 
 {
     public Transform MyGameObject;
 
     void Update()
     {
         // do stuff
     }
 }

UnityScript/Javascript version

 // gameobject base script MyGameObjectScript.js
 
 function Start() 
 {
     // first find the script using GetComponent
     MyOtherScriptType myscript = GetComponent.<MyOtherScriptType>();
     // with a reference, set your variable, this must be accessible and not private
     myscript.MyGameObject = transform; // pretend the MyGameObject is of type Transform
 }
 
 // The other attached script MyOtherScriptType.js
 
 public var MyGameObject:Transform;
 
 function Update()
 {
     // do stuff
 }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

While Loop Not Assigning Array 1 Answer

Assign Names Problem 1 Answer

How to clone a material in each instantiated prefab as Unique temporary material. 6 Answers

Instantiate multiple objects that follow the FirstPersonController 1 Answer

How to assign GameObject to a instantiated prefab via Script(C#)? 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