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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Kerihobo · Jul 10, 2013 at 02:50 AM · instantiatetransformvector

How do I control the transform.position of an instantiated prefab?

I am trying to make a road that will spawn itself as my player runs along it. Currently I have this:

 var spawnThis : Transform;
 
     function OnTriggerEnter (other : Collider)
     {
         Instantiate (spawnThis, transform.position, transform.rotation);
         Destroy (gameObject);
     }

it's the Instantiate line I am having trouble with, it just spawns it in the same place as the first object was, I want it to spawn at the next position.x integer. I was hoping something like this:

 Instantiate (spawnThis, transform.position.x ++, transform.rotation);

except that returns this error:

No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Transform, float, UnityEngine.Quaternion)' was found.

How can I go about this?

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

Answer by Cr0ss0vr · Jul 10, 2013 at 09:46 AM

I think i'd have just made a variable and stored the instantiated game object in it, that way you can just call the variable and do what you want with it, transform.position, or even rigidbody stuff...

 var spawnThis : Transform;
 var newSpawnThis;
 var speed;
  
     function OnTriggerEnter (other : Collider)
     {
         newSpawnThis = Instantiate (spawnThis, transform.position, transform.rotation);
         newSpawnThis.position.x += speed;
         Destroy (gameObject);
     }

that should work, right?

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 Kerihobo · Oct 25, 2014 at 03:37 AM 0
Share

Thanks guy :D

avatar image Cr0ss0vr · Oct 25, 2014 at 06:12 AM 0
Share

not a problem... i should have probably commented it, cause i dont really remember whats going on right here... i'm guessing spawnThis is the original object, and when something hits a collider its instantiated into newSpawnThis... not too sure whats getting destroyed though...

avatar image kingames123456789 · Jul 19, 2017 at 01:46 PM 0
Share

How should I change this line to transform position to the other gameobject that is not in the array :

void Spawn(){ Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position,Quaternion.identity); }

Something like this:

void Spawn(){ Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position,spawn.transform.position); }

avatar image
1

Answer by robertbu · Jul 10, 2013 at 03:08 AM

You can do something like this:

    function OnTriggerEnter (other : Collider)
     {
         var v3 = transform.position;
         v3.x += distance;
         Instantiate (spawnThis, v3, transform.rotation);
         Destroy (gameObject);
     }

...where 'distance' is either a variable you define or a hard-coded value.

Comment
Add comment · Show 5 · 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 Kerihobo · Jul 10, 2013 at 05:09 AM 0
Share

I tried something like that, except it gives me this:

"You are not allowed to call get_transform when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration."

and I am like "Whaaaaaaaaaaaaaaaat?"

How else do i store the position to a variable? :/

avatar image robertbu · Jul 10, 2013 at 05:26 AM 0
Share

The code above compiles for me, but I've seen this error before. Typically I would take this:

 var v3 = transform.position;

...and change it to this:

 var v3 : Vector3;
 v3 = transform.position;

...in order to get rid of the error.

avatar image Kerihobo · Jul 10, 2013 at 05:34 AM 0
Share

drats, that returned:

No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Transform, System.Type, UnityEngine.Quaternion)' was found.

avatar image Kerihobo Kerihobo · Jul 19, 2017 at 10:51 PM 0
Share

welp this was 4 years ago... looking back at beginner me makes me wanna smack me.

avatar image robertbu · Jul 10, 2013 at 06:26 AM 0
Share

You need to specify the type. You cannot do:

 var v3;

You need:

 var v3 : Vector3;

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

17 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

Related Questions

Setting parent of instantiated sprite 2 Answers

How do you instantiate an object to be oriented along a vector3? 1 Answer

Can't remove instantiated prefab 0 Answers

Variables stay overwritten in inspector 1 Answer

Bullet Translation question 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