- Home /
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()
{
}
}
So where is the question? what does not work? What happens instead?
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.
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.
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;
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
Both scripts are attached to the main gameObject?
Both scripts are enabled?
Did you attach a prefab to the public variable on the second script?
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!
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:
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?
Both scripts are enabled.
I am not using prefabs, just GameObjects. Do they need to be prefabs?
I will try this debug method.
Thank you!
$$anonymous$$aybe share some screenshots of the inspector of your enableOnComplete object and also the object the TextBox$$anonymous$$anager is on
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.
$$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.
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
![](https://koobas.hobune.stream/wayback/20220613010958im_/https://answers.unity.com/themes/thub/images/avi.jpg)