Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Merith · Jul 24, 2014 at 04:37 PM · c#multiplayerfind

What is the best way for scripts to find the player C#

Hello all, i'm currently in the process of adding multiplayer into my game and have found an issue where many of my scripts reference scripts attached to the player which is declared in the void Start. if the player is not in the scene when the scripts use void Start then the player is never found so none of my scripts work.

so long story shortish how can i make my scripts say "when the player has spawned

         GameObject Player = GameObject.Find("Player");
         CharacterControler characterControler = Player.GetComponent<CharacterControler>();

then continue with script". the only idea i had was making it check in the update and using something like "if player != null" but i imagine that would be very wasteful.

thanks for any help.

ps. im using Photon Networking if that makes any difference.

Comment
Add comment
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
0

Answer by etaxi341 · Jul 24, 2014 at 04:40 PM

You could use it in the Fixed Update so it wouldnt waste that much power. Or you could have one script that sets the player for all scripts and after thats done it deletes itself. So it only gets run if the player hasnt been found

Comment
Add comment · Show 5 · 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 Merith · Jul 24, 2014 at 05:02 PM 0
Share

thanks for the reply, is there any way to change variables in all scripts in the scene or at the very least all scripts with a certain name?

avatar image etaxi341 · Jul 24, 2014 at 05:53 PM 0
Share

I am sorry but I dont think so. You would have to "link" this Scripts to this main script. So you would do

     public Script1 script1;
     public Script2 script2;

and then you can add this scripts from the gameobjects in the inspector.

avatar image etaxi341 · Jul 24, 2014 at 05:56 PM 0
Share

But thats such a big expense. So i would just use the thing @robertbu said and put it in FixedUpdate. So it will only run 30times a second

avatar image robertbu · Jul 24, 2014 at 06:06 PM 1
Share

I disagree that 'FixedUpdate()' is the right solution. Only put it in FixedUpdate() if your references to the player is in FixedUpdate(). FixedUpdate() can run faster or slower than Update(), so if you put it in FixedUpdate() you could end an error when you attempt to access the game object.

avatar image Merith · Jul 24, 2014 at 06:14 PM 0
Share

well i changed everything up so it worked the way robertbu suggested and it took some tweaking but it works now :) thanks a ton for all the help (also i dont know how i can change his comment to an answer sorry)

avatar image
0

Answer by robertbu · Jul 24, 2014 at 05:07 PM

Note that GameObject.FindWithTag() is far more efficient than GameObject.Find(). So in Update() you should be able to:

 if (player == null) {
     player = GameObject.FindWithTag("Player");
 }
Comment
Add comment · 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 Merith · Jul 24, 2014 at 06:15 PM 0
Share

thanks so much for this, took a bit to get it done but it worked great

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GameObject.Find, When it is Called? 1 Answer

Multiplayer cant see host player C# 0 Answers

Multiplayer: client cant see host C# 0 Answers

A node in a childnode? 1 Answer

RPC called by OnPhotonPlayerConnected doesn't work 2 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