I don’t want to instantiate a prefab, I want to duplicate the exact gameobject I have the script attached to.
Apparently other people have thought about this and don’t love instantiating non prefabs, but can do it: http://answers.unity3d.com/questions/957617/how-to-clone-a-gameobject-in-runtime-not-with-inst.html
I don’t want a hidden “original” one because I’ve got too many things popping in and out of my script already. I’d really like to clone it at an exact moment in the game state, before I reset the original object to have no audio components.
Here’s what I’ve tried so far:
private GameObject answerClone;
Vector3 clonePosition = new Vector3(0, 0, 0);
answerClone = Instantiate(gameObject, clonePosition, Quaternion.identity);
I also tried :
answerClone = Instantiate(this.gameObject, clonePosition, Quaternion.identity);
Both get this error.
Assets/Scripts/PlayerFinalWord.cs(172,13): error CS0266: Cannot implicitly convert type `UnityEngine.Object’ to `UnityEngine.GameObject’. An explicit conversion exists (are you missing a cast?)
Ah, so I need to explicitly cast this as a gameobject. This site showed me how: http://answers.unity3d.com/questions/254195/c-cast-object-to-gameobject-not-working.html
This code now does the trick:
GameObject answerClone = (GameObject)Instantiate(answer, clonePosition, Quaternion.identity);
However, I don’t see the object on my screen , because it’s still being affected by the commands to its original object, “answer”. uh oh.
Things I’m reading that haven’t inspired me but might have the right answer:
Maybe I really do need an early invisible OTHER answer. Ugh. So many invisible things running around, spawning other invisible things! I guess I need to just create an entirely new game object and give it the TextMesh and AudioSource components of Answer at the right moment in time.
Alright so I made an empty gameobject with 3 children: NotebookEntryOne, NotebookEntryTwo, and NotebookEntryThree. When a word gets put into the notebook, it’ll go through and index the children, and keep count. This makes use of a code I’ve used before, to make an object have a yellow border around it from a yellow child object:
endingHighlight = ending.gameObject.transform.GetChild(0);
So here it is:
if (other.tag == “Notebook“)
Transform notebookOne = (Transform)notebookEntries.gameObject.transform.GetChild(notebookCount);
notebookOne.transform.position = new Vector3(notebookOne.transform.position.x, notebookOne.transform.position.y, 5f);
TextMesh notebookOneText = notebookOne.GetComponent<TextMesh>();
notebookOneText.text = thisText.text;
notebookCount += 1;
Problems that happened on the way:
- I forgot that GetChild returns a Transform
- forgot that I need .text after a TextMesh to convert it from a Textmesh to a string
- forgot the “new” in front of my Vector3 – easy fix
- Note that I’m just moving the entry from behind the notebook image to in front of it (with the z transform.position changing from 10 to 5).
I’m super excited about this – looks kind of like magic! Now to do the same for the other possible slot that answers can end up in – the garbage!