Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 c4mino · Sep 01, 2020 at 07:07 AM · gameobjectreplaceanother scriptoverwrite

Replace public GameObject in SCRIPT1 with dragged public GameObject from SCRIPT2?

Hello all, still struggling with what I think should be a simple problem but I'm just not getting it. I have a public GameObject (enableOnComplete) in SCRIPT1 that needs to be populated/overwritten by a public GameObject (enableOnCompleteUpdater) that I drag into the field in SCRIPT2. Thanks for any help you may be able to provide!

 //SCRIPT1

 public class TextBoxManager : MonoBehaviour
 {

     public GameObject enableOnComplete;

     void Start()
     {
         //This is the line I'm struggling with. I want it to replace enableOnComplete in script1
         //with whatever is dragged into enableOnCompleteUpdater in script2
         enableOnComplete = enableOnComplete.GetComponent<ActivateTextAtLine>().enableOnCompleteUpdater;
     }
 }

 //SCRIPT2

 public class ActivateTextAtLine : MonoBehaviour
 {

     public GameObject enableOnCompleteUpdater;

     void Start()
     {
     }
 }








Comment
Add comment · Show 10
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 Captain_Pineapple · Sep 01, 2020 at 07:13 AM 0
Share

So where is the question? what does not work? What happens instead?

avatar image c4mino Captain_Pineapple · Sep 01, 2020 at 07:17 AM 0
Share

Hey there, thanks for taking the time. What happens instead is basically nothing at all. If I leave enableOnComplete empty, it stays empty, rather than being populated with enableOnCompleteUpdater's object. If I put an object into enableOnComplete, it stays as that object, instead of being replaced by enableOnCompleteUpdater's object. It seems enableOnCompleteUpdater has no effect on enableOnComplete as it should.

avatar image ShadyProductions c4mino · Sep 01, 2020 at 07:28 AM 0
Share
 void Start()
 {
     Debug.Log(enableOnComplete.GetInstanceID());
     enableOnComplete = enableOnComplete.GetComponent<ActivateTextAtLine>().enableOnCompleteUpdater;
     Debug.Log(enableOnComplete.GetInstanceID());
 }

Try this, and check if your ID remains the same or not. If it changes, that means the script does work.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by RodrigoSeVeN · Sep 01, 2020 at 05:04 PM

Just in case Unity is having trouble with this line where the object somewhat replaces itself, try storing it first:

 enableOnComplete = enableOnComplete.GetComponent<ActivateTextAtLine>().enableOnCompleteUpdater;
 
 /// Instead try:
 
 var nextObject = enableOnComplete.GetComponent<ActivateTextAtLine>().enableOnCompleteUpdater;
 
 enableOnComplete = nextObject;
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 Marioooo · Sep 01, 2020 at 03:43 PM

Hello! i don't get the problem... there's a runtime error? or it just doesn't replace?

Follow this checklist

  1. Both scripts are attached to the main gameObject?

  2. Both scripts are enabled?

  3. Did you attach a prefab to the public variable on the second script?

  4. Did you attach the CORRECT prefab?

if nothing above helps, then first create a variable to store the script before that line and debug the moment when it tries to read the variable... you'll immediately know why it isn't working. Hope this Helps!

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 c4mino · Sep 01, 2020 at 04:46 PM

Hi Mario. Thanks for taking the time to help! I appreciate it.

The problem is it doesn't replace. Per @ShadyProductions advice, when I run:

  Debug.Log(enableOnComplete.GetInstanceID());
  enableOnComplete = enableOnComplete.GetComponent<ActivateTextAtLine>().enableOnCompleteUpdater;
  Debug.Log(enableOnComplete.GetInstanceID());

It gives me an instance ID for the first one, and then a nullref instead of a new instance ID for the second one.

For your checklist:

  1. No, unfortunately. I cannot have both scripts on the main GameObject. This is sort of the issue I am trying to overcome. I need a GameObject1 declared by drag/drop field in Script1 to be overwritten/fully replaced by a GameObject2 declared by drag/drop field in Script2. Is this not possible then?

  2. Both scripts are enabled.

  3. I am not using prefabs, just GameObjects. Do they need to be prefabs?

  4. I will try this debug method.

Thank you!

Comment
Add comment · Show 4 · 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 ShadyProductions · Sep 01, 2020 at 05:12 PM 0
Share

$$anonymous$$aybe share some screenshots of the inspector of your enableOnComplete object and also the object the TextBox$$anonymous$$anager is on

avatar image c4mino ShadyProductions · Sep 01, 2020 at 08:51 PM 0
Share

https://imgur.com/eguos8r

https://imgur.com/RUtO740

What I want to happen is that any object I drop into enableOnCompleteUpdater will overwrite the object in enableOnComplete.

@$$anonymous$$arioooo I turned everything into prefabs, and now enableOnComplete will wipe out the gameobject that is there, but it replaces it with None (No GameObject). At least it's doing something now! It comes back with ID 0 in the console.

avatar image ShadyProductions c4mino · Sep 01, 2020 at 09:10 PM 0
Share

$$anonymous$$y guess is that these objects are prefabs and not in the scene, thats why it doesn't work. I partly agree with $$anonymous$$arioooo, this is not a good design approach.

avatar image Marioooo · Sep 01, 2020 at 05:48 PM 0
Share

i think i can understand what you trying to do... this is a Design problem not a coding problem...

First: no, a gameobject can't replace itself...

What are you trying to do? a message box?

if this is true then why not simply make a main gameobject that holds a text component and then create an array of texts and replace the text on the component?

if this is not the case, then simply create a controller with a reference for a PREFAB and then just instance the prefab while holding a reference to destroy it if you need...

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

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

Change the mesh of a gameObject 1 Answer

SOLVED - String replace % with " in C# 1 Answer

Problem passing a GameObject to another script 0 Answers

In Unity 5, how do I replace files instead of appending "1" and duplicating? 4 Answers

Replace GameObject with different FBX 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