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 RSud · Apr 05, 2011 at 08:20 PM · cameradynamictargetassign

Setting camera target after I instantiate the gameobject?

Newbie question on syntax...

I have the mainCamera in the game and I have attached the smoothfollow script to it.

I instantiate the gameobject when the game starts dynamically that I want the camera to follow so I can't set the target in the inspector.

After I instantiate the gameobject what is my syntax in javascript to assign the gameobject I just instantiated to the mainCamera's smoothfollow script?

Thanks for any help.

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

Answer by DaveA · Apr 05, 2011 at 08:33 PM

Something close to this:

var sf : SmoothFollow = Camera.main.GetComponent(SmoothFollow);
if (sf)
  sf.target = myNewObject.transform;
Comment
Add comment · 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
1
Best Answer

Answer by efge · Apr 05, 2011 at 08:34 PM

You could use the function GetComponent and assign the instantiated GameObject to the variable "target" of the script:

var script : SmoothFollow; script = Camera.main.GetComponent("SmoothFollow");

var prefab : Transform; var clone : Transform; clone = Instantiate (prefab, ... );

script.target = clone;

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 Cobalt-9 · Nov 21, 2012 at 10:02 PM 0
Share

I'm having the same problem as the person who asked the question, and I originally had the prefab set as "var prefab: Gameobject" because when I set it to a transform it would instantiate clones over and over, but when I set it to a GameObject it gives me the error "cannot convert UnityEngine.GameObject to UnityEngine.Transform. Any idea what's wrong with it?

avatar image TheArtRobot · Jul 08, 2013 at 11:04 PM 0
Share

I'm having the problem described in the title here, but I'm not understanding the suggestions.

I'm thinking there's something wrong with my order of operations because everything looks correct in the Inspector on play but the camera doesn't follow the player.

Can anyone see what I'm doing wrong?:

     var useCamera : Camera;
     var player : GameObject; //player doesn't exist until prefab is instantiated
     var spawnPnt : Transform;
     
     function Awake () 
     {
         SetCamera();
         SpawnPlayer();
     }
     function LateUpdate () 
     {
         useCamera.transform.LookAt( player.transform );
     }
     function SetCamera()
     {
         useCamera = GameObject.FindWithTag ("$$anonymous$$ainCamera").camera;
     }
     function SpawnPlayer()
     {
         Instantiate(player, spawnPnt.position, spawnPnt.rotation);
     }
avatar image TheArtRobot · Jul 09, 2013 at 05:05 PM 0
Share

I found a solution to my particular problem. $$anonymous$$y camera was set to look at the prefab and not the instantiated (clone), so I added

 var camLookAt : Transform;

and:

 function SetCamLookAt()
 {
     camLookAt = GameObject.FindWithTag ("Player").transform;
 }

to run after SpawnPlayer();

Then updated:

 useCamera.transform.LookAt( camLookAt );

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

2 People are following this question.

avatar image avatar image

Related Questions

Moving Camera With 2 Players 3 Answers

Camera focusing on an object specified in a script 1 Answer

Camera re-target 2 Answers

Assign parent object to target 2 Answers

set the cloned prefab as target ???????? 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