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 esitoinatteso · Oct 02, 2014 at 01:07 PM · gameobjectinstantiategetcomponentreferenceclone

Reference an Instance of an Object?C#

Hi there! I've got a question about a specific problem related to a topic which seems to be hard to understand.

That topic is .GetComponent<>(); The question may sound silly but before I got overwhelmed by errors I thought I learned this lesson well... but now I don't know what to think anymore!

And the question is:

What do I have to do to access/reference/manipulate an instantiated prefab/GameObject/thing?

Now something more specific ( i'm keeping this generic enough so that any answer may easily be understood by others in my same needs):

I've got scriptA attached to objectA, scriptB attached to objectB and scriptC attached to objectA. scriptA has a public "slot" in the editor in which i put objectB. This is a simple way to reference another GameObject.

Then I put my objectA with its two scripts inside the Hierarchy, so that it exists.

When I execute in scriptA an Instantiate(objectB) I'm making a Clone of that GameObject, am I right? But what if my scriptC tried to access a variable of scriptB?

I've set up the references "properly", and by properly I mean that I followed the .GetComponent<>(); method. Obviously I get a null reference exception in this case.

  • know that exception is right, it does make sense: the hierarchy contains no objectB so there's not a scriptB either. But the Hierarchy do contains an objectB(Clone), which has a scriptB.*

So what would I have to do to "talk" to that Clone with my scriptC?

I also tried the static workaround, even if it's not smart when you have multiple clones on screen. It didn't worked.

The only thing I managed to do to solve this issue is to put objectB inside the Hierarchy too, thus disabling the Instantiate execution inside scriptA of objectA. This way I just have to make a regular Find & GetComponent with scriptC to access stuff in scriptB. But this is not what I wanted in the first place.

My apologies, I know this argument has been already explained in tons of examples, but I just couldn't get it. Needless to say that a link or some lines of code would be very appreciated. Thanks in advance for your kindness.

P.S.: I can provide you my own code if you think that might improve your answers, but it's just a coded version of what I stated above.

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

Answer by Quaker_SDR · Oct 02, 2014 at 01:27 PM

As you said,

Script C and Script A is in same game object, While you instantiate object b clone get the reference of the instantiated game object in Script A.

 GameObject g= Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity) as GameObject;

Now if you want to get the access of the instantiate game object in script C. You can get the reference of the script A in script C using getcomponent. With that reference you can access the instantiated object.

Comment
Add comment · Show 1 · 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 esitoinatteso · Oct 21, 2014 at 09:38 PM 0
Share

I've been familiarizing with the clone-thing stuff, which is to say the whole instantiation/instance/referencing process, and finally I was able to understand it once and for all! I thank you for the time you took to write your answer, which is correct and has been helpful!

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

27 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 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

instantiated gameobjects(clone) don't have components of its original gameobject. 1 Answer

How to SetActive(false/true) on an instantiated object, being a clone? 1 Answer

GameObject reference breaks 1 Answer

Copying GameObjects with reference types 1 Answer

Creating a reference gameobject of a prefab 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