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 ahmed.yahia · Sep 12, 2013 at 07:50 PM · prefabtargetcloneassigncloned

set the cloned prefab as target ????????

well, in the hierarchy i have a camera with smoothFollowTarget.js , in my project i have a Cube-Prefab which cloned during the game ... is there any way to assign the Cube-Prefab as camera target when it is cloned during the game ???

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 Seth-Bergman · Sep 12, 2013 at 07:56 PM

when you create the clone, you can store a reference to it in a variable:

instead of-

  Instantiate(myCube,whatever.position,whatever.rotation);

use-

  var newCube = Instantiate(myCube,whatever.position,whatever.rotation);

then you can use this reference to set the target:

  var cameraScript = GetComponent(smoothFollowTarget);
  cameraScript.target = newCube;

EDIT: you shouldn't mix c# and javascript in the same project, but if you must, keep in mind that the communication can only go ONE WAY, so the js can see the cs, but then the cs CANNOT see any js scripts.. or vice versa.. just pick one and convert the rest to that.. BUT, based on your current info so far, you don't need anything to SEE the camera script, so as of now here's how to do it:

the cs script you provided:

 void OnConnectedToServer(){
 SpawnPlayer();
 }
  
 private void SpawnPlayer(){    
 //change this line.. we declare a GameObject variable to store the new object reference
 GameObject newObject = Network.Instantiate(playerPrefab, new Vector3(0f, 5f, 0f), Quaternion.identity, 0);

 //find the camera, store a reference to the script
 smoothFollowTarget theCamera = GameObject.Find("Name of camera object").GetComponent<smoothFollowTarget>();

 //set the variable
 theCamera.target1 = newObject.transform;
 }

the js script you provided: (no change)

 public var target1 : Transform;
       
 function Start () {
 
 transform.position.y = -647.2977;
 transform.Rotate(270 ,0 ,0 );
  
 }
  
 function Update () {
  
 var x = target1.position.x;
 var z = target1.position.z;
  
 transform.position.x = x;
 transform.position.z = z;    
 }

for this to work with both cs and js though, you would need to put the js script into a special folder that will compile earlier..

http://wiki.unity3d.com/index.php/Special_Folder_Names_in_your_Assets_Folder

just create a folder in your base Assets folder called "Plugins" and put the js script in there

Comment
Add comment · Show 6 · 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 ahmed.yahia · Sep 12, 2013 at 09:03 PM 0
Share

create.cs which contain this code in c#

   var newCube = Instantiate(myCube,whatever.position,whatever.rotation);


smoothfollowtarget.jc which contain target variable in javascript

so,its give me more errors

avatar image ahmed.yahia · Sep 12, 2013 at 09:03 PM 0
Share

create.cs

 void OnConnectedToServer(){
   SpawnPlayer();
     }
     
      
 private void SpawnPlayer(){
         
  Network.Instantiate(playerPrefab, new Vector3(0f, 5f, 0f), Quaternion.identity, 0);
  
         
         
         
         
 }


smoothfollowtarget.js

 public var target1 : Transform;
 
 
 
 
 
 
 function Start () {
 
 
    transform.position.y = -647.2977;
    transform.Rotate(270 ,0 ,0 );
 
 }
 
 function Update () {
 
 var x = target1.position.x;
 var z = target1.position.z;
 
    transform.position.x = x;
    transform.position.z = z;
    
   
 
  
 }
 


and thanks for your answer

avatar image ahmed.yahia · Sep 12, 2013 at 10:10 PM 0
Share

Any ideas?

avatar image Seth-Bergman · Sep 12, 2013 at 10:59 PM 0
Share

mixing c# and javascript is not a good idea. you should pick one and convert the rest to that. BUT, I will update my answer based on your code...

avatar image ahmed.yahia · Sep 13, 2013 at 12:11 AM 0
Share

Thanks for your patience, it works greatly but i made a small modify to this

 GameObject newObject = Network.Instantiate(playerPrefab, new Vector3(0f, 5f, 0f), Quaternion.identity, 0);



will be like

 var newObject = Network.Instantiate(playerPrefab, new Vector3(0f, 5f, 0f), Quaternion.identity, 0)as GameObject;

finaly i'll take your advice and converting my scripts to c# and thank you again......

Show more comments

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

16 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

Related Questions

Assign an instantiated GameObject? 1 Answer

Clone not following Target 1 Answer

Instantiate Clones Itself - Rather than Prefab 0 Answers

(Clone) from Instantiated Prefabs over Network 1 Answer

Cant delete clones of prefab 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