call a method to other gameobjects in scene from listener in Unity3D

0 votes
I am making a game in Unity3D. listener does not need to be in the scene as a component to listen to event. now i want call a method on other gameobject when i receive chat from server. but as listener is not in scene, i can't get any gameobject to call method. how do I achieve this?
asked Mar 24, 2015 in AppWarpS2 by patel.sagar2611 (34 points)

1 Answer

+1 vote
Hi,

You should keep listeners in a scene. Every time you switch a scene, remove listener from previous scene and add listeners from new scene.

Then you can easily use FindGameObject to find objects and call methods from their components.

Thanks
answered Mar 24, 2015 by Suyash Mohan (900 points)
Thanks for the answer.

actually I was using transform.GetComponent to get access to method of other class. it didn't work that way. but it works with GameObject.Find method even if I don't keep listeners in my scene. and I also figured that if I keep listener in scene, then also that instance(in my scene) of listener class is not called but internally appwarp's listener instance is accessed. because when I try to print(transform.name), It gives null reference exception.

how can I get scene instance of listener to be called automatically or the only way is to use GameObject.Find.
Hi,

Listeners are just classes. You have to use listeners as you would do with any other classes. You can communicate between listeners and other components as you would do with any other classes/components in your game. The important thing is that if you declared listeners as gameobject in one scene and try to access them in other it will not work because that gameobject will be destroyed while switching scene. So either you need to use don't destroy method or make listener as global singleton object.

The best approach is to created listeners in every scene. You can declare them as component and attach them to some gameobject in every scene. And then whenever you are switching a scene, remove the listener from previous scene and add listeners' reference from next scene.

For communication, you need to have reference to the receiving game object and it's component to call the required method.

Thanks
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...