• 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 /
  • Help Room /
avatar image
0
Question by Der_Kevin · Dec 07, 2015 at 03:28 PM · camerainstantiatespawn

set instantiated object as Transform target?

Hey! i want to set an instantiated Car as my camera target. the instantiation script looks like this at the moment:

 using UnityEngine;
 using LE_LevelEditor.Core;
 using System.Collections;
 
 namespace LE_LevelEditor.Example
 {
     public class ExampleGame_Game : MonoBehaviour
     {
         
         public GameObject TestCar;
         public GameObject Spawnpoint;
 
         private void Start ()
         {
         // find player start position
                 GameObject goPlayerStart = GameObject.Find("Objects/PlayerStartPosition");
                 Spawnpoint = GameObject.Find("Objects/PlayerStartPosition");
                 
                 Instantiate(TestCar, Spawnpoint.transform.position, Quaternion.identity);
 .
 .
 .

the TestCar Gameobject is a prefab called "MuscleCar" and its "Muscle Car(Clone)" when it gets instantiated. and my smooth follow script like this:

 using UnityEngine;
 using System.Collections;
 using LE_LevelEditor.Example;
 
 public class SmoothFollow2 : MonoBehaviour {
     public Transform target;
     public float smoothTime = 0.3f;
     
     private Vector3 velocity = Vector3.zero;
 
     void Start () {
         
         //lookAtVector =  new Vector3(0,lookAtHeight,0);
         target = GameObject.Find("GameLogic").GetComponent<ExampleGame_Game>().TestCar.transform;
         
     }
     
     void Update () {
         Vector3 goalPos = target.position;
         goalPos.y = transform.position.y;
         transform.position = Vector3.SmoothDamp (transform.position, goalPos, ref velocity, smoothTime);
     }
 }


but somehow it doesen't follow the GameObject "Muscle Car(Clone)" In the field "Target" is only a MuscleCar placed. i think thats the prefab right? can anybody tell me why it doesent look at the Instantiated Car?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bonfire-Boy · Dec 07, 2015 at 04:11 PM

It doesn't look at the instantiated car because TestCar (as referenced on line 14 of the second code bit of code) is your prefab, not the clone.

To access the clone you'll need to store a reference to it when you Instantiate it. So, add another GameObject variable to the Example_Game class, let's call it TestCarClone, and then change

Instantiate(TestCar, Spawnpoint.transform.position, Quaternion.identity);

to

TestCarClone = Instantiate(TestCar, Spawnpoint.transform.position, Quaternion.identity) as GameObject;

And then, when you want to reference the clone instead of the prefab, do so.

I would recommend giving your prefab fields names with the word 'prefab' in them, that way you're much less to make this (otherwise very easy) mistake.

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 Der_Kevin · Dec 07, 2015 at 06:20 PM 0
Share

ah, thank you very much!

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

39 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 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

Im trying to set a spawned enemy a transform on another script 0 Answers

Spawn prefabs from array in specific order given the size of prefab 1 Answer

gameobject doesn't spawn in the assigned random positions? 2 Answers

Instantiate only once in Update and be able to use the clone variable in a switch statement(in Update). 1 Answer

Best method to spawn objects in network 0 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