- Home /
How to replace a gameobject referenced in JS using C#
Please help!
What I have, is a javascript that manages a radar called "radarscript" that requires a game object called "anything" to be placed in "Transforms[0]". I then have a C# script that instantiates "Player". I then need to update the "radarscript" with the newly instantiated "Player" game object.
I have searched thoroughly and asked elsewhere but can't get an answer that doesn't baffle me. I've read the doc about GetComponent, but trying to incorporate the example just gives errors like "radarscript doesn't exist in the current context".
There must be an understandable way to replace "anything" with "player"
The javascript has the line 'Transforms[0] = GameObject.Find("anything").transform;'
Can I update "anything" to "player" with c# once "player" is instantiated?
Thanks
Answer by MajorParts · Nov 08, 2014 at 07:38 PM
This is the syntax I was after...
GameObject temp = GameObject.Find ("playerSHIP(Clone)/localSHIP");
GameObject.Find("RadarMgr").GetComponent<RadarScript>().Transforms[0] = temp.transform;
Are you answering your own question or did you want this as a comment on your question?
It is the answer. I got help from someone elsewhere and I thought I would update here with the correct syntax.
Answer by robertbu · Nov 03, 2014 at 10:13 PM
Transforms[0] = GameObject.Find("anything").transform;
Your question is confusing. You can execute the line you specify using 'Player' as long as a game object with the name 'Player' exists at the time the line is executed. If it does not exist, then this line will generate a null reference exception. Also note that by convention, variables should start with a lower case letter.
"radarscript doesn't exist in the current context".
Missing classes will happen if you don't have things compiled in the right order. The typical solution is to move any C# files that need to be access from Javascript into StandardAssets.
http://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html
thanks Robert. The problem has arisen due to the fact that the radar script was not meant for a multiplayer project. $$anonymous$$y player (or any player) only gets instantiated after the script has run. For single player projects, the game object can be stated in the inspector. For multiplayer, for now, I have to drag the player game object to the inspector once I've spawned. This obviously won't be possible if I build the project to an exe or webplayer. Therefore, I need to replace the "anything" with "player" by use of a script. I have already moved folders and files as searches have suggested. I'm not sure if you made a mistake typing, but it's the other way round...it's javascript that needs to be accessed from c#. I've been trying to add code to the c# script that handles the player spawn to update the radar javascript.
Your answer
![](https://koobas.hobune.stream/wayback/20220613170651im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to Check if a component Exists on a Gameobject. 3 Answers
when mouse is on a object do this 2 Answers
press e to speek -1 Answers
AddComponent() causes a "trying to create a MonoBehaviour using the 'new' keyword" warning 2 Answers
Runtime Add/Remove C# Script 2 Answers