• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by MajorParts · Nov 03, 2014 at 10:09 PM · c#javascriptgameobjectcomponent

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

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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;
Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Linus · Nov 08, 2014 at 03:18 PM 0
Share

Are you answering your own question or did you want this as a comment on your question?

avatar image MajorParts · Nov 08, 2014 at 11:39 PM 0
Share

It is the answer. I got help from someone elsewhere and I thought I would update here with the correct syntax.

avatar image

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

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MajorParts · Nov 03, 2014 at 10:42 PM 0
Share

thanks Robert. The problem has arisen due to the fact that the radar script was not meant for a multiplayer project. My 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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Get game component type? 1 Answer

Find a Component/GameObject Using an Interface Reference 2 Answers

Disabling A Script on a GameObject From a Different Script 2 Answers

Add component to a gameobject created through code. 1 Answer

Is it OK to use public accessers for unique gameobjects (C#) ? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges