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 daviddickball · Jun 01, 2015 at 07:59 AM · javascriptinstantiatereferencingorder-of-execution

Referencing instantiated player in camera's Start()

Hey guys, I'm instantiating my player in the Start() of my GameController script, and then referencing it in my Main Camera so that I can track the player's movement each frame with the camera.

BUT - When I reference the player by tag in the Start() of my camera script, it doesn't find it. I assume this is because the camera's Start() is run before the player is instantiated, so can't find it. So what should I do? Could I delay the camera so that it looks for the player AFTER it's instantiated? Should I instantiate the Main Camera too?

Here's my GameController script:

 private var playerSpawn : GameObject; // a dummy object to mark where player starts

 function Start () {
 
     playerSpawn = GameObject.Find("PlayerSpawn");
     var playerInstance : GameObject = Instantiate(
         Resources.Load("Player", GameObject),
         Vector3(playerSpawn.transform.position.x, 0, playerSpawn.transform.position.y), 
         playerSpawn.transform.rotation);
 }

And the camera script:

 public var player : GameObject;
 
 function Start () {
     player = GameObject.FindGameObjectWithTag("Player");
 }
 
 function Update () {
     transform.position.x = player.transform.position.x;
     transform.position.z = player.transform.position.z - 3.2;
     var camerapos = transform.position - player.transform.position;
 }

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
Best Answer

Answer by revolute · Jun 01, 2015 at 08:04 AM

One option is keeping camera script disabled (even in the scene so that Start() will never occur) until you have instantiated player.

The other option is adding the camera script after you have instantiated player.

Another option is adding Init() to camera script and invoke it after you have instantiated player. Of course, you would have to do if(player != null) check, but in my opinion, this is more reliable.

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 daviddickball · Jul 01, 2015 at 07:43 AM 0
Share

I'm not sure how to add the scripts in a specific order. I've accepted your answer but what I've actually done to solve the problem is decided not to instantiate the player at all, I'll just place it in the scene manually.

avatar image
0

Answer by Ryzokuken · Jun 01, 2015 at 08:09 AM

Do you know that Instantiate does not only create an instance, but returns the gameobject as well.

 GameObject go;
 go = instantiate(prefab) as GameObject



Comment
Add comment · Show 3 · 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 daviddickball · Jun 02, 2015 at 07:04 AM 0
Share

I don't think I'm quite knowledgeable yet in Unity to understand your answer. Are you saying there's a better way to do what I'm doing?

avatar image Ryzokuken · Jun 03, 2015 at 09:49 AM 0
Share

Actally, what I am saying is, that:

  1. You are instantiating a character.

  2. You are trying to get a reference to your character using GameObject.Find() , GameObject.FindObjectWithTag() and what not.

Actually, the Instantiate() function in Unity has multiple overloads, including one which returns the Object which you're instatiating, which can be easily typecasted to a GameObject.

Simply put,

Rather than:

 Instatiate(myPrefab);

You can:

 GameObject myVariable = Instantiate(myPrefab) as GameObject;


Hope This Helps!

avatar image daviddickball · Jul 01, 2015 at 07:42 AM 0
Share

Oh I see! yes that's much more sensible, thanks!

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying WaitForSeconds before Instantiating 2 Answers

Issues referencing variables between functions 1 Answer

How to Instantiate prefab as child? (Java) 1 Answer

Prevent instantiating on collider 1 Answer

Object instantiation Logic Javascript. 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