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 C.IV · Feb 21, 2014 at 03:32 AM · 2dinstantiatejavaclone4.3

Changing a variable on an instantiated prefab clone once created

Note: Would prefer code snippets in Java, but I can read C# if you prefer to use that.

I'm creating a 2D sidescroller with 4.3+ and am attempting to use instantiated blocks from 4 prefabs to create the ground and obstacles the player is going to encounter. These move across the screen when the player is "moving" but the player stays in the middle of the screen. The issue is the blocks hit the player and push him off the screen. If I use a linecast or collider I can get a single block to stop moving, but I want all blocks on the screen to stop moving if the player collides with any of them to simulate the player stopping.

Now I can get the instantiated objects to spawn moving or to spawn stationary, but I cannot get them to switch states once created. I've also tried using several variations of arrays to check all objects with the same tag and move them that way, but this has yielded no results nor does it generate errors.

Example of both codes:

 if(playerRunning == true && groundSpawnTimer >= 5)
     {
     var dirtBlock = Instantiate(GroundBlock, new Vector2(0,-1.1), Quaternion.identity);
     var dirtMove : BlocksMove = dirtBlock.GetComponent(BlocksMove);
     //dirtMove.blockMove = true;  //Commented out as this spawns blocks moving
     groundSpawnTimer = 0;
     }
 var tiles = GameObject.FindGameObjectsWithTag("Ground"); //Attempted array to move spawned blocks
 for(var ground in tiles)
     {
     if(playerRunning == true)
     {
     //ground.transform.position -= transform.right*7*Time.deltaTime; //Wasn't sure what I was supposed to reference here. Tried several. None worked.
     dirtBlock.transform.position -= transform.right*7*Time.deltaTime;
     }

I have all my components attached to a single game controller except the code to move the blocks which is contained in the prefabs. If there's a way to move this code to the controller to make it easier that's fine, but my main concern is stopping all blocks from moving upon collision (or setting playerRunning == false on collision to stop the blocks).

The only thing I've not tried to spawning new blocks rather than instantiating prefabs, but I think this would present the same issue in the long run.

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 C.IV · Feb 24, 2014 at 01:43 PM

I figured out a way via checking against the player's own code how to stop instantiated objects or update their status based on the player's status. By using the player as a set object (already in the scene) I placed the following code into the prefab block objects:

 function Update () {
 
 var playerState :  playerController = GameObject.FindGameObjectWithTag("Player").GetComponent(playerController);
 
 if(playerState.running == true)
     {
     transform.position -= transform.right*blockSpeed*Time.deltaTime;
     }
 }

By having every instantiated object seek out the gameobject tagged as "player" and then check to see if its running state was true (set by the game controller when the player pressed the run key) each instantiated object can then update itself without having to refer back to the prefab it was created from.

On larger games or if you have many hundreds of objects this is probably a bad way to do it, but it works for small numbers of objects without affecting performance.

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

18 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

Related Questions

How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer

Instantiated GameObject's components are disabled 3 Answers

Prefab clones behaviour 0 Answers

Instantiate GameObject at Random time is not working 1 Answer

How can I get the x position for the left(and right) of the screen? 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