Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by MichaelABC · Jan 19 at 11:34 AM · prefabspritespriterendererprefab-instanceclones

How to correctly change sprite on a prefab clone?

Hello Community, I have made a script so that when the Linecast between the mouse cursor and the player hits a gameobject ("Int_Molecule") it changes its sprite into a new one.

 public class CursorXhair : MonoBehaviour
 {
     public Player Player;
     public AttachmentController Molecule;
     //public SpriteRenderer spriteRenderer;
     public Sprite newSprite;
     public Sprite originalSprite;
  
     // Update is called once per frame
     void Update()
     {
         Vector2 playerPos = Player.transform.position;
         Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         Vector2 direction = mousePos - playerPos;
  
         transform.position = mousePos;
  
         Debug.DrawRay(playerPos, direction);
         RaycastHit2D mouse2Player = Physics2D.Linecast(mousePos, playerPos);
  
         if (mouse2Player.collider != null && mouse2Player.collider.tag == "Int_Molecule")
         {
             Debug.Log("mouse2Player is colling");
             Molecule.spriteRenderer.sprite = newSprite;
         }
         else
         {
             Molecule.spriteRenderer.sprite = originalSprite;
             Debug.Log("mouse2Player is NOT colling");
         }
     }
 }



Since I need multiple clones instantiation of this same object I create this script and assign it to a LevelManager. So I create a prefab as the original object to instantiate. The Linecast works as expected as you can see in the two images, however the clones do not update the sprite as expected. Interestingly the prefab in the prefab folder does update (see the last image), but the update is not reflected in the running game and the clones instantiations.

 public class MoleculeInstantiator : MonoBehaviour
 {
     public GameObject molecule;
     //public Transform moleculeLocation;
    
     // Start is called before the first frame update
     void Start()
     {
         Instantiate(molecule, new Vector2(-1, 6), Quaternion.identity);
         Instantiate(molecule, new Vector2(-11, 2), Quaternion.identity);
         Instantiate(molecule, new Vector2(9, -6), Quaternion.identity);
         Instantiate(molecule, new Vector2(14, 3), Quaternion.identity);
         Instantiate(molecule, new Vector2(-14, -7), Quaternion.identity);
         Instantiate(molecule, new Vector2(-17, 6), Quaternion.identity);
         Instantiate(molecule, new Vector2(17, -9), Quaternion.identity);
     }
  
 }



Is there something I am doing wrong or in the wrong order?

I am thinking that the best way would be to have the two scripts above as one attached to the prefab (and therefore the clones), but I preferred to keep the two scripts separated for better tidiness.

Would that solve it or am I missing the point?

alt text alt text

2in1.jpg (41.4 kB)
upload-2022-1-18-21-48-39.png (43.2 kB)
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 MichaelABC · Jan 19 at 11:39 AM 0
Share

"colling" should be "colliding", sorry for the typo...

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.

Follow this Question

Answers Answers and Comments

237 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

Related Questions

Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. 0 Answers

Changing Sprite won't work 0 Answers

Setting Sprite renderer mask property to 'visible inside mask' makes the Sprite image dissappear regardless of a mask. 1 Answer

Instantiate prefab by total int 1 Answer

How can we make a script to changer render order, based on players and enemies Y positions? 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