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 lurksway · Apr 01, 2018 at 02:19 AM · scripting problemscript.invokescene load

Script run twice after loadscene (restart)!

Hi, I'm a new into Unity. I want to make a life pickups that randomly generated in an area. I also want to have 1 life pickup only present at the field, so I use GameObject.Find to detect the prefab in the scene. After the life have been picked up, the next pickup will be 5 seconds later. The first life pickup spawns after 5 seconds the game started.

It all worked perfectly, but then I press my retry button and this where it goes weird. The first 5 seconds spawns 2 life pickup. After picking up the two life pickups, it will spawn 1 life pickups. After that, it will spawn 2 life pickups. My prediction is the script run two times after the scene is reloaded. Can someone help me why is this happening?

This is the C# Script for the life pickups (shield pickups)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ShieldSpawn : MonoBehaviour {
 
     public GameObject ShieldPrefab;
 
     public Vector3 center;
     public Vector3 size;
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         if (GameObject.Find("Shield(Clone)") != null)
         {
             // this object is on scene
             CancelInvoke("SpawnShield");
         }
         else
         {
             // this object is a prefab, not instantiate on any scene
             Invoke("SpawnShield", 5);
         }
     }
 
     public void SpawnShield()
     {
         Vector3 pos = center + new Vector3(Random.Range(-size.x / 2, size.x / 2), 0, Random.Range(-size.z / 2, size.z / 2));
         Instantiate(ShieldPrefab, pos, Quaternion.identity);
     }

This is the Retry Button Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class ButtonControl : MonoBehaviour {
 
     public void OnClick()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
     }
 }


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 melsy · Apr 01, 2018 at 05:27 PM 0
Share

Will there only ever be one picked up ? then if you pick it up , 5 seconds later it will spawn a new one? Is that what needs to be accomplished. ?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by melsy · Apr 01, 2018 at 05:41 PM

add this to an empty game object in the scene

make sure the Player has a rigidbody and Tag "Player"

 using UnityEngine;
 public class Spawner : MonoBehaviour
 {
     [SerializeField]
     Vector3 center, size;
 
     [SerializeField]
     GameObject prefab;
     GameObject spawnedShield;
 
     public void CreateNewShield()
     {
         Invoke("SpawnShield", 5);
     }
 
     void SpawnShield()
     {
         Vector3 pos = center + new Vector3(Random.Range
             (-size.x / 2, size.x / 2), 0, Random.Range(-size.z / 2, size.z / 2));
         spawnedShield = Instantiate(prefab, pos, Quaternion.identity);
     }
 }

add this to the shield prefab

 public class Shield : MonoBehaviour
 {
     // Add this into the shield script
     // Put a collider on the shield item and also make it trigger
 
     Spawner spawner;
     const string player = "Player";
     private void Awake()
     {
         // Not the best way but easy and since it only 
         // happens ever now and then doesnt hurt anything
         spawner = FindObjectOfType<Spawner>();
     }
 
     private void OnTriggerEnter(Collider other)
     {
         if(other.CompareTag(player))
             spawner.CreateNewShield();
     }
 }

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

140 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

Related Questions

How can i update a list in update function only when there is a change ? 2 Answers

How do I get an object to Instantiate infront of a specific object/player? 1 Answer

Why when getting component the variable is null ? 1 Answer

How do I make a photography function? 0 Answers

Show speed in UI 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