how do i remember a just instantiated prefab as a variable gameobject?
i want to assign a gameobject to my just instantiated prefab. the problem is. it needs to change depending on what lane the prefab spawned on. (each prefab needs to remember the object that spawned the prefab) i have tried this:
GameObject MonsterInstance = Instantiate(PrefabGreenMonster, SpawnPoint.position, SpawnPoint.rotation) as GameObject;
MonsterInstance.GetComponent<WalkingScript>().Target_LaneController = gameObject;
but sadly, for some reason i cant get it to work.
i need to find a way to send something to the instantiated prefab.
What does "can't get it to work" mean? An error? A crash? You can play the game but it doesn't work as intended?
You are pretty much just assigning a value to a variable, so the first thing that comes to $$anonymous$$d is that maybe the types don't match.
Is WalkingScript.Target_LaneController
of type GameObject? It needs to be since you are trying to assign a GameObject into it
the Target_LaneController
is a variable in the other script. it is meant to link back to the lane controller
script (the one with this code, and that will spawn the prefab). also WalkingScript
is the target script, i need access to that script so i can link it to the lane controller
.
basically, script_A needs to instantiate object_b with script_B. but when instantiating, it also needs to link script_B back to script_A, so that Script_B can read out some variables in script_A.
what i could not get to work, is any kind of connection to the script/gameobject i just instantiated. when instantiating, i can give it a location, add force to it so it shoots like a bullet. but i can not get a simple thing like any other kind of variable to the prefab.
Answer by ElDo · Oct 27, 2015 at 06:52 PM
Let's see if I got you right on this.
you could have your Script_A like this:
public class Script_A : NetworkBehaviour {
public int number=5;
void Start(){
GameObject o = Instantiate(Prefab,Position,Rotation) as GameObject;
o.GetComponent<Script_B>().scriptA=this;
}
}
Now your Script_B Needs a global(public) Variable of Type Script_A (in my example it has the Name "scriptA" so Script_A can link itself (with "this") to Script_B. Now after the new GameObject with Script_B attached has been spawned it can Access all global (public) Variables of Script_A:
public class Script_B : NetworkBehaviour{
public Script_A scriptA;
void Start(){
Debug.Log(scriptA.number);
}
}
Now your Script_B should print out the number 5 of Script_A
do i need to use NetworkBehaviour ins$$anonymous$$d of $$anonymous$$onoBehaviour for this to work?
i have re created this, but for some reason my script_B does not receive the connection to script_A. public Script_A scriptA;
always stays emptey, and Debug.Log(scriptA.number);
did not debug anything, (probably because he could not find anything.)
it does not Need to be an NetworkBehaviour. could you copy/paste your code here so I can crosscheck it?
script_A
public class LaneController : $$anonymous$$onoBehaviour {
//test
public int number = 5;
void Update ()
{
if (SpawnTestButton == true)
{
GameObject $$anonymous$$onsterInstance = Instantiate(PrefabGreen$$anonymous$$onster, SpawnPoint.position, SpawnPoint.rotation) as GameObject;
SpawnTestButton = false;
$$anonymous$$onsterInstance.GetComponent<WalkingScript>().scriptA = this;
}
}
script_B
public class WalkingScript : $$anonymous$$onoBehaviour {
//test
public LaneController scriptA;
// Use this for initialization
void Start ()
{
//test
Debug.Log(scriptA.number);
}
}
Well you should not spawn a Rigidbody as a GameObject! Where do you get your null Reference Exceptions from? What line of code/Variable?
yea, that was a little mistake on my part. but your code did the job. but for some reason i had to change all the game objects in script_A to rigidbody's. i still don't know exactly why i get null reference when they are gameobjects, and why they disappear when they are rigidbody's. but it works now.
as i said before, thanks for the help.
Your answer
Follow this Question
Related Questions
Referencing the Transform of the children of an Instantiated GameObject _PLEASE HELP!!! 1 Answer
I'm having a few issues with (Catmull)Splines that i need help with. 1 Answer
How do I Instantiate a prefab at the x axis position of where the UI button was clicked 1 Answer
Problem with Instantiate custom class 0 Answers
Problem with projectile direction 0 Answers