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 question was closed Mar 04, 2021 at 10:25 AM by saqlainniazi63 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by saqlainniazi63 · Mar 01, 2021 at 09:17 AM · scripting beginnerscriptingbasicsinstantiate prefabscriptingproblem

How to instantiate Road Prefab at desired position ?

Hello everyone I have successfully written script for road generation and it works perfectly but I want to instantiate Road Prefab at my desired x position please help me if anyone knows Here is the script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;


 public class RoadGenerator : MonoBehaviour {
 private float spawnz =  -6f;
 public GameObject[] prefabs;
 private float RoadLength = 109.4f;
 private int amountOfRoads = 6;

 private List<GameObject> roadsList;
 private Transform playertransform;
 void Start () {
 roadsList = new List<GameObject>();
 playertransform = GameObject.FindGameObjectWithTag("Player").transform;
 for (int i = 0; i < amountOfRoads; ++i)
       SpawnRoad(0);
 }
 void Update () {
 if (playertransform.position.z > (spawnz - amountOfRoads * RoadLength))
    {
     SpawnRoad(0);
     DeleteRoad();
    }
 }
 void SpawnRoad(int prefabIndex)
 {
 GameObject go;
 go = Instantiate(prefabs[prefabIndex]) as GameObject;
 go.transform.SetParent(transform);
 go.transform.position = Vector3.forward * (spawnz - 109.4f);
 spawnz += RoadLength;
 //adding gameobject to list
 roadsList.Add(go);
 }
 void DeleteRoad () {
 Destroy(roadsList[0]);
 roadsList.RemoveAt(0);
 }




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

  • Sort: 
avatar image
0
Best Answer

Answer by pauldarius98 · Mar 01, 2021 at 10:03 AM

Could you provide more information, like what is going wrong? Do you have errors or the roads are spawning at the same position?

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 saqlainniazi63 · Mar 01, 2021 at 11:46 AM 0
Share

Yes of course I have a ground which is at 0 x position and a player which is also at 0 x position but the player is on the left side of the ground and I have attached a swipe manager script to the player the problem is that the swipe function also starts from the left side of the ground and I want to spawn my ground according to the player such that the player should be in the middle of the ground please tell me that how can I change the x position of the ground which is to be instantiated ?

avatar image saqlainniazi63 · Mar 01, 2021 at 11:51 AM 0
Share

Roads are spawning perfectly but I want to spawn the ground at these positions (-5.54, 0, 0)

avatar image pauldarius98 saqlainniazi63 · Mar 01, 2021 at 12:07 PM 0
Share

Oh, I see. In that case the solution is quite easy, at line 32 you should have:

 go.transform.position = new Vector3(x, 0, spawnz - 109.4f);

And instead of x place the value that you need

avatar image saqlainniazi63 · Mar 04, 2021 at 10:21 AM 0
Share

Thanks sir this line of code is working fine but I did not replace this line on the line number 32 instead I did write this line after line number 32

 go.transform.position = new Vector3(x, 0, spawnz - 109.4f);

Follow this Question

Answers Answers and Comments

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

Trying to find the highest number than add it to itself. 2 Answers

Junior Programming Pathway scaling a cube 2 Answers

Animator Controller 0 Answers

Can someone explain calling other scripts in C#? 2 Answers

Can I load new information into GameObjects when a new scene is loaded? 3 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