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
1
Question by a_boy_with_hair · Jun 17, 2020 at 08:35 PM · variablesinstantiate prefabinstantiationinstantiate-game-object

Instantiate question

Hi, when instantiating an gameobject is there anyway I can have the values in the instantiated gameobject script be the same as another gameobject already in the scene? Here is my code:

 public class PlayerController : MonoBehaviour {
 
     public Interactable focus;
 
 }
 
 public class Arrow : MonoBehaviour
     {
         public PlayerController pc;
         public Interactable currentTarget;
     
         void Update()
         {
             currentTarget = pc.focus;
         }
     
     }

When I click on an gameobject in the game, the focus variable gets that object then the arrow prefab is instantiated but without having the currentTarget in the Arrow script, the same as the focus variable in the PlayerController script.

Sorry for the poor description of my problem.

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 FeedMyKids1 · Jun 18, 2020 at 12:17 AM

https://stackoverflow.com/questions/78536/deep-cloning-objects would show you about cloning objects in C#.

Because Monobehaviour doesn't allow constructors, you couldn't implement a copy constructor.

But since your values are public (and even if you make them private but give them public access through getters), you could put a method on the Arrow like

 public void MakeThisCopyOf (Arrow arrowToCopy)
 {
     this.pc = arrowToCopy.pc;
     this.currentTarget = arrowToCopy.currentTarget;
 }

but that would be a huge pain in the ass depending on how many fields you're copying.

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 FeedMyKids1 · Jun 18, 2020 at 09:55 PM 0
Share

I'm wrong.

Look at $$anonymous$$agso's answer.

Pass your original gameobject into the Instantiate method of $$anonymous$$onobehaviour.Instantiate and it will give you an exact clone, with your fields set the same and everything.

avatar image
2

Answer by Magso · Jun 18, 2020 at 09:33 AM

Instantiate returns an object type in other words anything the variable type is assigned to.

 GameObject NewArrow;
 NewArrow = Instantiate(...);
 NewArrow.GetComponent<Script Name>().currentTarget = currentTarget;
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 FeedMyKids1 · Jun 18, 2020 at 09:57 PM 1
Share

GameObject newArrow = Instantiate (ORIGINAL_ARROW_REFERENCE) as GameObject

and you don't even need to set the fields up.

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

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

"Quaternion.LookRotation" not working when Instantiate 1 Answer

GameObject variable points to instance instead of prefab when prefab gets instantiated 2 Answers

How to instantiate once when boolean changes 1 Answer

Instantiate an instance? 1 Answer

Tutorial for manual instantiation of prefabs yields null exception 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