Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Gamevara · Sep 20, 2015 at 07:42 AM · transformprefabposition

How to change a prefab position during play mode

Hello Masters and Gurus.

I'm practising Unity with Flappy Bird demo. I want to adjust the gate on fly so it will get harder to pass them. The gate (columns) is a prefab. I have tried different methods and leave uncommented the one which doesn't give any errors and also change the position of the prefab but not playing columns. I also cut unnecessary parts like lower column away.

Could you please assist me? How to change prefab position while playing?

 public class GameControlScript : MonoBehaviour 
 {
     public Transform upperGate;
     public float upperPosition = 3f;
 
     //Try 1: Also tried with Transform.position.  Which one is correct?
     /*
     Vector3 upper = transform.position; 
     upper.y = upperPosition;
     */
 
     void Update()
     { 
 //Also tried with different level, but that was a way too complicated
 /*        if (score >= 5) 
         {
            Application.LoadLevel("level-2");        
         } */
 
         if (score >= 5 ) 
         {
             //upperGate.position = upper; //Try 1
             upperGate.position = new Vector3 (0, upperPosition, 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

· Add your reply
  • Sort: 
avatar image
0

Answer by Gamevara · Sep 28, 2015 at 12:02 PM

Hi,

I managed to solve the problem. Instead of try to change prefab I created different prefabs and changing them during the game. Here is how I created it.

 using UnityEngine;
 using System.Collections;
 
 public class ColumnSpawnScript : MonoBehaviour 
 {
     public GameObject startPrefab;        //the column game object: start
     public GameObject level2Prefab;        //the column game object: level 2
     public GameObject level3Prefab;        //the column game object: level 3
     public int columnPoolSize = 5;        //how many columns to keep on standby
     public float spawnRate = 3f;        //how quickly columns spawn
     public float columnMin = -1f;        //minimum y value of the column position
     public float columnMax = 3.5f;        //maximum y value of the column position
 
     GameObject[] columns;                //collection of pooled columns
     int currentColumn = 0;                //index of the current column in the collection
     GameObject columnPrefab;
 
     void Start()
     {
          columnPrefab = startPrefab;
         //deactivate level2 and level3 prefab
         level2Prefab.SetActive (false);
         level3Prefab.SetActive (false);    
         setPool ();
         //starts our function in charge of spawning the columns in the playable area
         StartCoroutine ("SpawnLoop");
     }
 
    //removed the standard stuff here.
 
     public void ChangeLevel (int score)
     {
         if (score < 2)
         {         
             columnPrefab = startPrefab;
         }    
         else if (score >= 2 && score < 9)
         {                
             level2Prefab.SetActive(true);
             columnPrefab = level2Prefab;
             startPrefab.SetActive(false);
             setPool ();    
         }
         else if (score >= 9)
         {
             level3Prefab.SetActive(true);
             columnPrefab = level3Prefab;
             level2Prefab.SetActive(false);
             setPool ();
         }
     }
 
     void setPool ()
     {
         //initialize the columns collection
         columns = new GameObject[columnPoolSize];
         //loop through the collection and create the individual columns
         for(int i = 0; i < columnPoolSize; i++)
         {
             //note that columns will have the exact position and rotation of the prefab asset.
             //this is because we did not specify the position and rotation in the 
             //Instantiate() method call
             columns[i] = (GameObject)Instantiate(columnPrefab);
         }
     }
 }


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

2 People are following this question.

avatar image avatar image

Related Questions

Grab position from instantiated object 1 Answer

Why prefab revert does not work for position and rotation? 1 Answer

Instantiated prefab position 5 Answers

GameObject position and localPosition not changing in hiearchy, only in script. 0 Answers

Setting Position of Spawned Prefab 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