Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Alp-Giray-Savrum · Feb 01, 2014 at 03:31 PM · transformprefabobjectrandomassign

How to assign Transform to prefab ?

I've been working for 2 days and i still didn't succeed :(

There's 2 spaceships i use...

alt text

and ther are using same code;

I wrote a smooth look at code using random methods. Bu I can't define my transform objects to prefab.

As you see;

alt text

I'm trying to drag drop them but it's not available.

 var targetsArray : Transform[];
 var damping = 6.0;
 var smooth = true;
 
 private var target = -1;
  
 function Start() {
 target = Random.Range(0, targetsArray.Length);
 }
 
 function LateUpdate () {
         if (smooth)
         {
             // Look at and dampen the rotation
             var rotation = Quaternion.LookRotation((targetsArray[target]).position - transform.position);
             transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
         }
     }

and that's my code. So , Is there another method can i use ?

P.S. = I'm using 1 scene in my game. One level, one scene.

ekran alıntısı.jpg (124.8 kB)
ekran alıntısı.jpg (121.4 kB)
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
5

Answer by robertbu · Feb 01, 2014 at 04:19 PM

A Prefab is like a blueprint for a game object (including scripts and children). It doesn't exist in the scene until it is Instantiated(). You cannot drag and drop scene items onto a prefab since it doesn't exist in the scene and can be Instantiated into any scene. You can drag and drop other object within the prefab...that is you can fixup internal links within the prefab, just not external ones.

As for your problem, you are going to have to figure out an alternate way to make these links. Something like:

 var targetsArray : Transform[] = new Transform[4];

 var damping = 6.0;
 var smooth = true;
  
 private var target = -1;
  
 function Start() {
     targetsArray[0] = GameObject.Find("1Follower").transform;
     targetsArray[1] = GameObject.Find("2Follower").transform;
     targetsArray[2] = GameObject.Find("3Follower").transform;
     targetsArray[3] = GameObject.Find("4Follower").transform;
 
     target = Random.Range(0, targetsArray.Length);
 } 

An alternate method would be to use the tag manager to give all the followers the same tag. Then you could do:

 var targetsArray : GameObject[];
 var damping = 6.0;
 var smooth = true;
 
 private var target = -1;
 
 function Start() {
     targetsArray = GameObject.FindGameObjectsWithTag("Follower");
 
     target = Random.Range(0, targetsArray.Length);
 } 

Note this alternate method has the entries of the 'targetsArray' as game object, not transforms.

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 Alp-Giray-Savrum · Feb 01, 2014 at 04:35 PM 0
Share

I'll check :D Thanks

avatar image surfuay · Sep 25, 2019 at 02:48 AM 0
Share

did this work for you i'm dealing with the same issue

avatar image Lolypop79 · Jul 01, 2020 at 03:50 AM 0
Share

This doesn't work, I have the same problem

avatar image Bunny83 Lolypop79 · Jul 01, 2020 at 04:18 AM 0
Share

This approach does work perfectly fine for the question that was asked here. If it doesn't work in your case, you don't have the same problem. This question is 6 years old and the code is written in UnityScript which is no longer supported in the latest Unity versions. Regardless of the used language the concept does still work the same way. Your comment is just a pointless bump of an ancient, answered question.

avatar image
0

Answer by Spark59op · Apr 11, 2021 at 07:16 PM

Ok try this... It worked for me For example i am using my transformable object "player" with tag "Player"

First give reference

GameObject player;

Then in start function

player = GameObject.FindWithTag("Player"); target = player. transform;

It will work perfectly

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.

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

Instantiate a random prefab at an objects location 3 Answers

Can't add Transform to an Array on Prefab. 1 Answer

The variable othertransform of Prefab has not been assigned 2 Answers

Random position? 1 Answer

Avoid getting same object as previous one with Random.Range? 2 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