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 dpkonofa · Nov 27, 2012 at 07:02 AM · instantiatevariablesobjects

Getting an object from another script...

I'm using a script that was written by someone else. In this script, the player is instantiated and the main camera follows this instance of the player model. When the player dies, that instance is destroyed and a new instance is created when the level restarts. I need to setup some items that follow the player but I'd like to put them in another script. How can I access the player that's instantiated by the first script so that I can read its position to set the following objects to have the same position (which I can then move to where I need it)?

I've tried using:

player = firstScript.Find("ThePlayer");

or

player = firstScript.Find("PlayerChar_01(Clone)"); (this is what the GameObject is labelled as in the project on runtime)

How can I do this? I keep getting errors about needing to create objects for non-static members and I have no idea what that means... Help!!

Comment
Add comment · Show 1
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 AlucardJay · Nov 27, 2012 at 07:13 AM 0
Share
 playerScript = GameObject.Find( "ThePlayer" ).GetComponent( PlayerScript );
 playerScript.someVar = 10.0;

http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html

http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ThePunisher · Nov 27, 2012 at 09:24 PM

Use the code that alucardj gave you, BUT first you will need to find the code that instantiates the player's prefab then rename the object to "ThePlayer" or whatever suits your needs.

You can do this with the following code.

 //Somewhere there will be code that looks similar to the following.
 GameObject go = GameObject.Instantiate(playerPrefab) as GameObject;
 go.name = "ThePlayer"; //* Add this line of code.

Then in the script where you are making modifications to the player, use:

 GameObject player = GameObject.Find("ThePlayer");

An alternative is to find the playerPrefab and set its Tag (which can be found as a drop down in the inspector when you have the playerPrefab selected) and choosing the "Player" tag. Then in your new script all you have to do is:

 GameObject player = GameObject.FindGameObjectWithTag("Player");


Let me know if you have any questions.

Comment
Add comment · Show 4 · 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 dpkonofa · Nov 28, 2012 at 01:36 AM 0
Share

Hey Rob... I've already tried something similar to this but GameObject.Find("Player"); always yields a "null" result. I don't get any errors except for a NullReferenceException when I use this. I've got a Debug.Log(player) setup and it always reads "null".

I can see that the player is now named "Player" but how do I get this current script to see things from the other one? It seems like Script2 isn't able to find a GameObject that was instantiated in Script1... :(

avatar image dpkonofa · Nov 28, 2012 at 02:15 AM 0
Share

Ok... I did a little testing and it looks like the reason that I'm getting a "null" result is that the layer GameObject is created in Script1's Start() function. The Find command is also being run in Script2's Start() function so that I can make changes to "player" like you have in your script. Since Script1 and Script2 are both running their Start() commands at the same time, Script2 gets a null result from Script1. If I move the definition to the Update() function, I no longer get a null result.

So...how do I only run this assignment once but do it after Script1 finishes its Start() function???

Thanks, Rob!! I really appreciate the help!!

avatar image dpkonofa · Nov 28, 2012 at 02:41 AM 0
Share

Sorry for the rush of updates. I added a delay to the script with the Script Execution Order settings but I'm not sure if that's the right way to do it. It's working but I want to make sure that this is the most efficient way to do this...

avatar image ThePunisher · Nov 28, 2012 at 02:48 AM 0
Share

You need to be using Awake and Start methods properly to avoid complications like the one you have.

Awake and Start are only called once per script, BUT Awake is always called before Start. Use Awake for initialization of all your scripts, then Start for transfering/modifying data between other scripts. This methodology ensures that you always initialize every component before perfor$$anonymous$$g any actions to them.

So with that in $$anonymous$$d, change the script that instantiates your player so that the Start method is now an Awake, and you should be fine. Let me know if that presents other errors.

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems with variables across scripts 0 Answers

Variable not Assigned 1 Answer

Instantiate 3 guiTextures at the same 1 Answer

Mouse Over, Mouse Down, and Instantiate. 1 Answer

How do I reference an object's rotation through an attached script? 1 Answer


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