• 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 El-Deiablo · Apr 21, 2016 at 09:14 PM · getcomponentspriterendererfliparray of gameobjects

Access SpriteRenderer for random sprite pulled from array (trying to flip sprite if a spawned on a certain side)

I am new to this community and it is getting extremely frustrating seeing all of my questions unanswered and without any comments. I'll continue to try!

I have an enemy spawner that spawns enemies in 1 of 6 spawn points. The spawn points are on the left and right side of the screen. If the enemy is spawned on the right side I want it facing the left side as it runs across. I have done a flip head script where I have used the flipX method, but I am having a difficult time incorporating this into my enemy spawner script mainly due to the script being attached to an empty Gameobject and the spawner using arrays rather than one specific sprite. I am so close and have tried many different approaches, but am stuck. Some help would be appreciated!

 Here's my code:
 
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TempSpawner : MonoBehaviour{
 
     public float runningPlayerSpeed; //player speed
     public Transform []spawnPoints; //for spawn points
     public float spawnTime = 2f; 
     public GameObject [] Players; //array of players(3 total)
     public SpriteRenderer mySpriteRenderer;//trying to reference selected Players' sprite render
 
     public float minSpeed;
     public float maxSpeed;
     private float currentSpeed;
 
     void Awake () 
     {
         mySpriteRenderer = GetComponent<SpriteRenderer>();
 
     }
 
     void Start(){
         
         InvokeRepeating ("SpawnPlayers", spawnTime, spawnTime);
 
         currentSpeed = Random.Range (minSpeed, maxSpeed);
 
         if (spawnPoints[3]||spawnPoints [4]||spawnPoints [5]) {
             mySpriteRenderer.flipX = true;
         }
     }
 
     void Update(){
         float amountToMove = currentSpeed * Time.deltaTime;
         transform.Translate (Vector3.right  * amountToMove);
 
         int spawnIndex = Random.Range (0, spawnPoints.Length);
         int objectIndex = Random.Range (0, Players.Length);
     

//if enemy spawns on right side of screen - flip the sprite so that it is facing the player

         if (spawnPoints [spawnIndex ].position .x==10){ 
             
             //Players [objectIndex].transform.localScale.x  == -1f;
             mySpriteRenderer.flipX = true;
             //GameObject.FindGameObjectsWithTag ("Running Players");
 
         }
     }
 

//spawn random player in a random spawn location

     void SpawnPlayers(){
         int spawnIndex = Random.Range (0, spawnPoints.Length);
         int objectIndex = Random.Range (0, Players.Length);
         Instantiate (Players [objectIndex], spawnPoints [spawnIndex].position, spawnPoints [spawnIndex].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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Changing sprite image without loss of performance 1 Answer

Accessing Animator Component from tagged objects in an Array in Start and then triggering its bool in Update 1 Answer

How do I acces a specific GameObject within the terrain???? 0 Answers

Can't add sprite to instantiated GameObject 2 Answers

How to detect 1 gameobject in an Array 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