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 Zodijac · Aug 27, 2020 at 01:25 PM · scripting problemspawning

Can you trigger/run code from a unspawned objcts, that isnt in the scene yet?

Hey!

I´am working on a game, where things get faster over time (the BG and the Obsticals, the player is just moving right and left). I have a Code for my BG and My Obsticals: Obstical:

 public float obsticalspeed = 2.0f;




 private void Start()
 {
     
 }



 void Update()
 {
     float y = transform.position.y;

     y = transform.position.y - obsticalspeed * Time.deltaTime;

     transform.position = new UnityEngine.Vector2(transform.position.x, y);


     obsticalspeed += 0.1f * Time.unscaledDeltaTime;

 }


BG:

 public float speed = 2.0f;

 // Update is called once per frame
 void Update()
 {
     float y = transform.position.y;

     y = transform.position.y - speed * Time.deltaTime;

     transform.position = new UnityEngine.Vector2(transform.position.x, y);

     if(transform.position.y <= -10f)
     {
         transform.position = new UnityEngine.Vector2(0, 9.94f);
     }

     speed += 0.1f * Time.unscaledDeltaTime;
     
 }



The Problem is, that the obsticals will only start to higher their speed, when they are spawned (each does that seperatly, so all obsticals will only start higering their speed, when they are spawned), so they wont have the same speed as the BG, because the BG stays the gameobject in the see and will only be teleported to the right position.

How could you fix that? maybe start the script for every object that will be spawned in the future, so they can start counting up, when the scene starts?

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
1

Answer by xxmariofer · Aug 27, 2020 at 01:56 PM

if the speed should be shared you could simply access the BG speed from the other script, but you could also make the variable static

 public static float speed = 2.0;
Comment
Add comment · Show 9 · 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 Zodijac · Aug 27, 2020 at 02:43 PM 0
Share

Hey!

Thanks for the very quick answer, but how do i access the other script?

I have never used the GetComponent stuff before

Thanks!

avatar image xxmariofer Zodijac · Aug 27, 2020 at 05:38 PM 0
Share

there is no need of using GetComponent create a variable niside the class and drag and drop from the inspector

avatar image Zodijac · Aug 27, 2020 at 05:48 PM 0
Share

Sorry, i really dont know what you mean? In which script and what should i drag and drop in where?

avatar image Captain_Pineapple Zodijac · Aug 27, 2020 at 06:11 PM 1
Share

static variables are handled per class not per object instance. So if your class "$$anonymous$$yClass" has a public float speed you can access that from anywhere by just calling $$anonymous$$yClass.speed. This way you can give a value to a class and access it anywhere without calling GetComponent anywhere. This ofc implies that this value is unique in your complete project. There can not be 2 values for/in this variable.

avatar image Zodijac Captain_Pineapple · Aug 27, 2020 at 06:22 PM 0
Share

Thanks, It is working!

Show more comments

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

241 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

SteamVR - Spawning Prefab on Controller,SteamVR - Spawning Objects on Controller 0 Answers

Spawn and Destroy problem snake like 2 Answers

Spawn an object with additional parameter 1 Answer

How do i give Coordinates (x, y) to an Array 1 Answer

Script causing lag 1 Answer


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