- Home /
How to instantiate a prefab declared in one script, from another script
This is script one:
public GameObject chatContent;
//this gameobject is referenced in the editor
public void Start()
{
AddMessage("This is ok");
}
void OnEnable()
{
EventSystemManager.StartListening("messageArrived", AddMessage);
}
public void AddMessage(string message ) {
print("One");
GameObject remoteMessage = Instantiate(localMessagePrefab, chatContent.transform) as GameObject;
print("Two" );
remoteMessage.transform.GetChild(0).GetComponentInChildren<Text>().text = message;
print("Three");
DestroyMessage();
}
The method AddMessage executed in Start method works nice, and also i can execute AddMessage several time in the script. The problem is that if i execute this method AddMessage from another script it only get up to the print("One") in the method but the prefab is never instantiated. I have tried to do it by events and also referencing gameobject from the editor but nothing works. The method is executed but the instatiation fails with no errors.
This is script two with events:
private async void onMessageHandler(string e)
{
serverMessage = JsonUtility.FromJson<serverMssg>(e);
EventSystemManager.TriggerEvent("messageArrived", serverMessage.normmsg);
await miaApi.queueAnim(serverMessage.normmsg);
}
TIA!
In principal it should work fine. Just check through your code. Debug the execution and step through to see what happens at the line you believe to be failing.
Answer by JonPQ · Aug 23, 2019 at 11:29 PM
if it prints one, but not two, there is an error on the line inbetween. Try putting a debug breakpoint on that line and debugging it to see what is going on. Its likely that your ChatContent or LocalMessagePrefab is null , has been destroyed somehow, or was null to start with. If you are using unity 2018/2019 and new prefab flow. make sure your references are actually being saved to the prefab, (they might look ok in scene, but might be null in the prefab)
First of all thanks for the reply. Im using unity 2018.4.6f1. I have check all the references and all seem to be ok (also in prefab). Later i tried to debug with visual studio (breakpoint) but im not really sure what im looking for ( I am not used to using it), so i just comparing the same breakpoint from start method (that works) and from the eventTrigger.
So for me it looks like nothing is exactly null (only remote$$anonymous$$essage but it is ok), but maybe it is not getting the exactly same objetcs (It is my conclusion since the value is not exactly the same).
Also some things are in red but im not sure what it means.
Start method debug
EventTrigger Debug
Ok so what you are looking at is the the current state of variables that exist in that scope and the call stack.
Did you stop before line 14? in the first code example as the remote message is null.
Sorry i forgot to upload the image with breakpoint , i exactly stop in line 14.
@JonPQ so far, the only thing that i found strange is that the transform that i use to instantiate the prefab is gone, it does not appear in debug mode and also if I try to print transform.position (for example) nothing happens, even no errors. If I do it from the same script (several time in diferent moments) it works but if I launch the event trigger or execute the method by reference from another script it doesnt. I also tried to attach both scripts in the same object but there was no difference. $$anonymous$$ay is a behavior of unity , but i dont get it....
Answer by YetAnotherKen · Aug 24, 2019 at 07:32 PM
Simple answer would be to either pass the reference to the object as a parameter to a method in the other script which stores it there, then that script can use it's reference. If you want to modify the original, you can pass it by reference using the ref keyword. Kind of like:
method(ref objectReference);
...
void method( ref ObjectType objectReference) {
do stuff with objectReference.
}
Oh, the other method I was thinking of was having it, and the class it is in public so that the other class can declare a reference and use it, kind of like:
ObjectType objectType = OtherObject.GetComponent<ObjectType>();
void method () {
do stuff with objectType.objectReference. }
i.e.
public class TestObj
{
public string Name { get; set; }
}
class Program
{
static void ChangeName(TestObj obj)
{
obj.Name = "New Name";
}
static void $$anonymous$$ain(string[] args)
{
var testA = new TestObj {
Name = "Some name"
};
ChangeName(testA);
Console.WriteLine(testA.Name);
}
Writes: "New Name"
... to console, not "Some name"
You dont need to use ref if its a class which it probably is. Its passed by reference anyway.
I think is not exactly what i need.... my case is like:
I receive a message via websocket (it works) Later i pass this message to a "Chat" in other script/object and the message is passed, the problem is that the object "Chat" doesnt instance the prefab is like it doesnt exist but i does because i can instance the same prefab from itself in the same "play".