Referencing a variable from another script
The problem I'm having is. I need to be able to generate Gameobjects with LineRenderers attached (or added to an empty Gameobject upon instantiation). I then want the newly instantiated LineRenderer object to reference some variables from the playerMover script which instantiated it.
I've seen examples where people use GameObject thePlayer = GameObject.Find("ThePlayer"); OR PlayerScript playerScript = thePlayer.GetComponent();
but my issue is that the script which instantiates the linerenderer object is used on multiple objects in my game and the tags for each of these objects are often the same "Offense" or "Defense" so I'm concerned that using Find or GetComponent will pickup the wrong 'parent' (don't think that's the right use of parent but its a similar relationship).
I want the playerMover script to instantiate a new gameobject with linerenderer and then pass it a few variables. The variables can be passed to the new game object immediately in void Start()
Can someone show me some code which will work for this.
Answer by hexagonius · Oct 02, 2015 at 10:04 AM
inject the info. after instantiation, grab the script from it in your playermover. create a public SetVariables method in there,or Init... whatever, and pass it either every info as a parameter, or create a data container which gets filled and passed. in your case, the indirection of waiting for Start and look for whoever instantiated is not necessary.
I think this is the best solution. I have now encountered another problem with it.
I have declared public GameObject line; public LineCreator motionLR, playLR;
the following code executes in the main body and gives me an error:
motionLR = (LineCreator)Instantiate (line, Vector3.zero,Quaternion.identity);
motionLR.SetPosition (0, firstPos);
motionLR.SetPosition (1, secondPos);
In my LineCreator script I have a public function SetPosition() this piece is working.
The error I get is: "Cannot cast from source type to destination type"
I looked up the error and there is a range of different reasons why this might be happening. Am I not casting this properly?
Again, I just want to create a LineRenderer and give it a bunch of parameters which will draw a line on screen. It seems very simple yet this is giving me more problems than the rest of the project combined. Any thoughts are welcomed.
line is a gameobject, not a LineCreator. you need to run GetComponent on the instantiated gameobject. btw there is a generic version of instantiate available, no need for the cast at all.
How do I then modify: motionLR.SetPosition (0, firstPos); motionLR.SetPosition (1, secondPos);
Because it is telling me that SetPosition() is not a valid function on a gameobject. That makes sense its not. Can you give me some example code for how I could do this?
Your answer
Follow this Question
Related Questions
Interchangeable animations in a script? 0 Answers
Multiple Enemy Stats and Scripts 1 Answer
Should I inherit from Monobehaviour if the use of my C# script is only to hold script references? 2 Answers
Draw Line Between Distance Joint 2D and Position 0 Answers
Get Audio Source On Another Object 1 Answer