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 MysticalMrCool · Nov 20, 2020 at 10:58 AM · velocityprefabsobject pool

Instantiated Game Objects' Script Won't Update

I am currently developing my first game (falling platformer) and I've got an object pool of platforms that randomly spawn as the character falls. I have set the game up so the camera remains static while the platforms move up. In an attempt to create the illusion of falling, I have set up an invisible collider about halfway down the screen so that when the player lands on it, the velocity of the platforms should increase. However, the velocity variable in my platform behaviour script attached to my platform prefabs will not update.

Here is my platform behaviour script which is attached to my platform prefabs as well as my platform generator game object:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlatformBehaviour : MonoBehaviour
 {
     public float riseSpeed;
 
     Rigidbody2D rb;
 
     public GameObject fallCollider;
     FallCollider colliderScript;
 
     // Start is called before the first frame update
     void Start()
     {
         riseSpeed = 2;
         rb = GetComponent<Rigidbody2D>();
 
         colliderScript = fallCollider.GetComponent<FallCollider>();
     }
 
     // Update is called once per frame
     void Update()
     {
         Rise();
 
         if (colliderScript.playerTouching == true)
         {
             riseSpeed = 4;
             
         } else
         {
             riseSpeed = 2;
         }            
     }
 
     private void Rise()
     {
         rb.velocity = new Vector2(0, riseSpeed);
     }
 }

And here is my collider script which is attached to the invisible collider:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FallCollider : MonoBehaviour
 {
     public bool playerTouching;
 
     private void OnCollisionEnter2D(Collision2D collision)
     {
         if (collision.gameObject.tag == "Player")
         {
             playerTouching = true;
         }
        
     }
 
     private void OnCollisionExit2D(Collision2D collision)
     {
         if (collision.gameObject.tag == "Player")
         {
             playerTouching = false;
         }
     }
 
 }

When I run the game and pause it when the player is on the collider, I can see the riseSpeed variable updates for the PlatformGenerator object but does not update for each of the instantiated platforms. Any guidance would be extremely appreciated as I have been racking my head over this for the last few days.

Comment
Add comment · Show 1
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 Llama_w_2Ls · Nov 20, 2020 at 02:28 PM 0
Share

Use GameObject.Find() in the start() method of your PlatformBehaviour script to assign the fallCollider to your instantiated object. @$$anonymous$$ystical$$anonymous$$rCool

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Shameness · Nov 20, 2020 at 11:15 AM

Since you never initialized fallCollider field in PlatformBehaviour:

Your generated platforms don't have reference to fallCollider.

On the other hand, your generetor object have reference to fallCollider because I guess you set it manually using editor.

Comment
Add comment · Show 1 · 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 MysticalMrCool · Nov 21, 2020 at 10:38 AM 0
Share

Yes you're right I was trying to make the reference in the editor which wasn't working. After making the reference using FindWithTag() it worked perfectly. Thanks for your answer :)

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

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

Problems about prefabs and velocity affected by scripts 0 Answers

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

I made a rocket! Help! 2 Answers

Velocity from Rigidbody and from Transform don't match for small dt 1 Answer

Move Particles with Object but in world space (character controller) 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