Assignement object
Hi !
I hope that someone can resolve my problem.
In my project, I have a lot of scenes, and in each of these scenes I collect some informations, and I have one class per scene that collect information.
For exemple in the first scene I collect user's name and email. So I have a class ID :
public class IdUser : MonoBehaviour {
public string name;
public string email;
/* Some method */
}
And I have also a class that "don't destroy on load" and is present in each of these scene that collect information of all scenes. Something like this :
public class SuperClass : MonoBehaviour {
public IdUser myIdUser;
/* Some method*/
}
But this is my problem, when I am in my first scene (to collect IdUser) and I call this function inside mySuperClass, it doesn't work :
public class SuperClass : MonoBehaviour {
public IdUser myIdUser;
public void savemyIdUser() {
myIdUser = (IdUser) GameObject.Find("IdUser").GetComponent<IdUser>();
}
This method doesn't do anything, it doesn't assign the current object in the scene to my attribute in SuperClass.
If I print myIdUser it gives me "Null" but when I print (IdUser) GameObject.Find("IdUser").GetComponent<IdUser>();
It prints the refenrece to the object.
I hope that is understandable, you can ask me question if it doesn't.
Answer by TheSkyWolf · May 12, 2018 at 03:48 PM
What is your gameobject's name which you attach IdUser script? GameObject.Find search by gameobject name. Also I did not get where you assign myUserId. Do you print myUserId or myIdUser? I did not see myUserId variable.
The game object attach is the same name "IdUser", it's an empty game object with just script. In fact what is very strange is when I print GameObject.Find("IdUser"), it find my object
Oh yes sorry, I juste did a mistake when I wrote the question, I will correct, but yes I print myIdUser
Your answer
Follow this Question
Related Questions
Creating an object using a prefab, with parent, text etc.. 1 Answer
How to include speed trigger creation option in this script. 0 Answers
Why the object using Vector3.MoveTowards is not moving down ? 1 Answer
How to remove a specific area of a gameobject? 1 Answer
How to call an override function if it was overrode. If not, just call the parent's function 2 Answers