- Home /
Can I Access Objects or Scripts in an Additive Level?
I have a quick question, that maybe I can get some explanation on.
I currently have one level, which loads 3 additive levels. What I did was made my entire GUI out of additive levels, but I want to send notifications to that Additive level.
Is it possible to access a script running in an Additive Level from the Main Level that I loaded?
Answer by DrRocker87 · Sep 21, 2012 at 07:17 PM
@mikebelotti - Thanks! Didn't even think about that one.
For now, I just have the MainLevel finding the game object in the Additive Level by Name and calling its function:
GameObject.Find("Manager_Tasks").GetComponent<TaskMgr>().AddTask(1);
I dont think there will be any major mix ups as long as I keep my naming structure intact.
Thanks for all the help everyone!
Answer by DaveA · Sep 12, 2012 at 07:43 AM
I dont see why not, have you tried it?
I have tried something like this.
In the Additive level I declare a public function in a script (Notification.cs):
public void SendNotification(string text){
}
In the $$anonymous$$ain Level I tried calling that script, but the way I usually call a different script doesnt work.
I usually use: object.GetComponent().SendNotification("Here");
But I wouldnt have access to an object would I?
Round two:
I have a public class attached to my additive level.
public GUITexture Background;
Vector3 StartPos;
Vector3 EndPos;
float i = 0.0f;
public IEnumerator Transition()
{
StartPos = new Vector3(.1f, 1.0f, .2f);
EndPos = new Vector3(.1f, .9f, .2f);
float t = 10.0f;
while (t < 1.0f)
{
t += Time.deltaTime * (Time.timeScale/1.5f);
Background.transform.position = Vector3.Lerp(StartPos, EndPos, t);
yield return 0;
}
}
Now, I acces the script in the $$anonymous$$ain Level with a button:
void Start(){
PublicClass newPC = new PublicClass();
}
void OnGUI(){
if(GUI.Button(new Rect(100,100,100,100), "Slide In")){
StartCoroutine(newPC.Transition());
}
}
But when I do this, its making me declare and set the Background to the GUITexture. I have it setup in the Additive Level, why would i need to set it up again in the $$anonymous$$ain Level?
I think I am missing something here. I dont want to create a NEW class, I want to use the one that is was added in the Additive level.
Hmm. This is where Javascript (UnityScript) may help because it can resolve things like that. C# likes to have things declared up front. So some thoughts:
try Javascript or
have your 'new' class be 'available' to the first level. Put it in Plugins folder or make a dummy object or something
Use Send$$anonymous$$essage ins$$anonymous$$d of GetComponent
Ok, so I used the Send$$anonymous$$essage and did some reading on it, but its not working.
$$anonymous$$aybe I have something wrong.
So, in the "$$anonymous$$ain" level, lets say I have button that when clicked calls:
Send$$anonymous$$essage("AddTask", "Task1", Send$$anonymous$$essageOptions.DontRequireReciever);
And in the 'Additive' Level I have a $$anonymous$$onobehavior Class called Task$$anonymous$$gr.cs that has a 'public' function 'AddTask(string task)' with a simple debug statement in it.
Shouldn't the Send$$anonymous$$essage go through all the current objects in the combined levels and find 'AddTask' and complete it? Or do I still have to define a GameObject to which I send to?
Any help would be appreciated.
Thanks!
Your answer
Follow this Question
Related Questions
Accessing other objects 2 Answers
How do you add levels to your game? 1 Answer
Converting object into script 1 Answer
Additively loaded scene in game remains in editor? 0 Answers