• 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
1
Question by Goreleth · Nov 08, 2016 at 02:30 PM · unity 5instantiateprefab

LookAt method not working on instantiated objects.

Hello all. I am new to game development, and have been playing around with some concepts I've learned so far. I am having an issue instantiating a prefab that has a transform.Lookat() method. When I manually place one of these prefabs, it performs as intended, following my target, but when I instantiate them at runtime they don't follow the target. The rest of the script works (they also instantiate projectiles and propel them forward), just not the LoookAt() method. Do I not understand how this method works?

Here are the pertinent bits:

 void Update () {
 
         time += Time.deltaTime; 
         transform.LookAt(target.transform); 
 
         if(time > 2.0) {
 
             time = 0;
             GameObject p = (GameObject)Instantiate(projectile, transform.position, Quaternion.identity);
             p.GetComponent<Rigidbody>().AddForce(transform.forward * force); 
             Destroy(p, 3);
                 
         }
         
 }

This controls the launcher object, which creates a projectile and fires it at the target.

 void nextLevel() {
 
         
             Vector3 randV31 = new Vector3(Random.Range(-35.0f, 35.0f), 1.50f, Random.Range(-35.0f, 35.0f));
             
             Vector3 randV35 = new Vector3(Random.Range(-35.0f, 35.0f), 1.50f, Random.Range(-35.0f, 35.0f));
             transform.position = GameObject.Find("start").transform.position;
             Instantiate(scoreSphere, randV31, Quaternion.identity);
             Instantiate(launcher, randV35, Quaternion.identity);
 }

This bit of script is attached to the player and when a certain condition is met, it instantiates the launcher object, which is the prefab in question. Any help you could provide would be very appreciated.

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

Answer by RickshawDerpyDerp · Dec 13, 2019 at 04:30 PM

Greetings from the future, @Goreleth! I don't think you are providing enough information because you never show us how you are assigning the target. However, others (myself included) recently had this same problem and it is likely that the instantiated object's name is not what you think it is going to be. For example, if you have a prefab whose name is "MyPrefab", than the first instance of this object that Unity creates with Instantiate() is called "MyPrefab (Copy)". So using target = GameObject.Find("MyPrefab").transform; fails. It is possible that target = GameObject.Find("MyPrefab (Copy)").transform; could succeed (unverified) but I opted to use target = GameObject.FindGameObjectWithTag("Player").transform; instead. For more complicated scenes, you may need to use other methods, such as searching the contents of containers.

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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Want to instantiate Prefab in its parent. 1 Answer

Unity gets hang and nothing work in it, even I have to close it from the task manager 0 Answers

How do I Instantiate a prefab at the x axis position of where the UI button was clicked 1 Answer

Create gameobject in other scene... 0 Answers

Use FPSController/FirstPersonCharacter to Rotate a scene with a Joystick 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges