• 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 /
  • Help Room /
avatar image
0
Question by polan31 · Jul 16, 2019 at 08:47 PM · rotation2d gameprefabsclones

How to get access to the clone object?

2D c#

I have a few prefabs that generate clones. I added a rotation script to each object. I would like to add a button to the scene, with the help of which I will be able to Start Coroutine into every clone's object with specific tag. How to do it? A normal search a tag or the use of the option "other. (Function name)" doesn't work. Can someone help me solve this problem?

Rotation script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class Rotation : MonoBehaviour {

 public float speed;
 public float speed2 = 0f;
 private Rigidbody2D rb2D;



 // Use this for initialization
 void Start () {
     
 }

 // Update is called once per frame
 public void Update () {

     transform.Rotate (0, 0, speed);
 }

 public void Stop (){

     StartCoroutine(SpeedZero());
     Debug.Log ("ZEROOOOO");
 }

 IEnumerator SpeedZero()
 {
     transform.Rotate (0, 0, speed2);
     yield return new WaitForSeconds(20);
     transform.Rotate (0, 0, speed);
 
 }

}

Spawn script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SunSpawner : MonoBehaviour {
 
 
     public GameObject[] theSuns;
     public Transform generationPoint;
 
 
     private int sunSelector;
     private float sceneHeight;
 
     float distance = 0.5f;
 
 
 
     Vector3 maxWidthPoint;
     Vector3 minWidthPoint;
 
     //Radious base on circle collider radious
     float lastSunRadious = 2f;
 
 
 
     void Start (){
         minWidthPoint = Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height / 2f));
         maxWidthPoint = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height / 2f));
 
         float bottom = Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).y;
         float top = Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height)).y;
         sceneHeight = top - bottom;
 
         Debug.LogFormat("Scene height: {0}, minWidthPoint: {1}, maxWidthPoint {2}", sceneHeight, minWidthPoint, maxWidthPoint);
 
 
     }
 
         
     
     public void Update (){
 
         if (transform.position.y < generationPoint.position.y) {
 
             sunSelector = Random.Range(0, theSuns.Length);
             float currentSunRadious = theSuns[sunSelector].GetComponentInChildren<CircleCollider2D>().radius * theSuns[sunSelector].GetComponentInChildren<CircleCollider2D>().transform.localScale.x * theSuns[sunSelector].transform.localScale.x;
             float distanceBetween = distance + lastSunRadious + currentSunRadious; //Random.Range(lastSunRadious+currentSunRadious, sceneHeight);
             float sunXPos = Random.Range(minWidthPoint.x + currentSunRadious, maxWidthPoint.x - currentSunRadious);
 
             Vector3 newSunPosition = new Vector3(sunXPos, transform.position.y + distanceBetween, transform.position.z);
 
             transform.position = newSunPosition;
             lastSunRadious = currentSunRadious;
             Instantiate (theSuns [sunSelector], transform.position, transform.rotation);
 
 
         }
     }
         
 }
 
 


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 pyramidhead01 · Jul 16, 2019 at 09:00 PM

Let me know if this makes sense.

Make a variable List of these types of enemies and put it in the script that the OnClick() method is in:

 public List<GameObject> objects = new List<GameObject>();

Add them either in the inspector or in that spawn script you have:

 transform.position = newSunPosition;
              lastSunRadious = currentSunRadious;
              Instantiate (theSuns [sunSelector], transform.position, transform.rotation);
              objects.Add(theSuns[sunSelector];

Then on that OnClick() method, do something like this:

     public void OnClick()
     {
         int roll = new int;
         if (objects.Count > 1)
         {
             roll = Mathf.RoundToInt(Random.Range(0, objects.count)
             objects[roll].SpeedZero();
             return;
         }
            else if (objects.Count > 0)
                     {
             objects[0].SpeedZero();
                     }
     }


After I posted this, I realized I never answered your question! Sorry, I get distracted!

When do you need something to stop? Will a bool need to be true, a player standing inside/outside of something, will a certain value need to be 0 or 100, need to have something in inventor/solve puzzle?

It sounds like you are trying to stop a specific sun, maybe when a task in the game is completed I guess? This solution will stop a random sun of course, but without knowledge of when a sun would need to stop, I can't tell you how to reference that sun on the onclick method.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

136 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

Related Questions

the button in the bottom of scene don't work 1 Answer

Walking around a sprite 1 Answer

[Solved] Problem with achieving rotation relative to current acceleration value 0 Answers

I have searched like the whole Internet for this lol (get Value of specific Prefab(clone)) 2 Answers

Movement/Rotation Methods Don't Work Together 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