• 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 BmoreG · Jul 13, 2020 at 07:40 PM · scripting problemscriptableobjectscritping

OnMouseDown instantiates object in different spot

So I'm making tic-tac-toe game for practice and I created a square prefab that has a collider on it that I put in each block of the game board. I created a "Buttons" script that is attached to the squares that uses OnMouseDown.

     PlayerText playerText;
     SpawnSymbols spawnSymbols;
 
     private void Start()
     {
         spawnSymbols = FindObjectOfType<SpawnSymbols>();
         playerText = FindObjectOfType<PlayerText>();
     }
 
     private void OnMouseDown()
     {
         spawnSymbols.SpawnPlayerSymbol();
         GetComponent<Collider2D>().enabled = false;
         playerText.UpdatePlayerText();
     }

I initially did all of my instantiating within this script and it worked perfectly. The X's and O's were spawning where they were supposed to. But I then decided to put the instantiating on a separate script for better readability. This script looks like:

 [SerializeField] GameObject playerX;
 [SerializeField] GameObject playerCircle;
 
     Players player;
 
     private void Start()
     {
         player = FindObjectOfType<Players>();
     }
 
     public void SpawnPlayerSymbol()
     {
         if (player.playersTurn)
         {
             Instantiate(playerX, transform.position, transform.rotation);
             player.playersTurn = false; ;
         }
         else
         {
             Instantiate(playerCircle, transform.position, transform.rotation);
             player.playersTurn = true;
         }
     }

I also attached this script to the Square prefab so the transform should be exactly the same as it was on the previous script because they are both attached to the same objects, but for some reason when I click it instantiates the the symbols in one block no matter which block I click on. I have no idea what's going on, but I have a feeling it has something to do with the way OnMouseDown works. I'm sure it's something blatantly obvious, but I just can't see it. Any help would be greatly appreciated!

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
Best Answer

Answer by BmoreG · Jul 13, 2020 at 08:27 PM

So I seemed to have figured it out. It would appear using spawnSymbols = FindObjectOfType<SpawnSymbols>(); was the cause behind the issue. I changed it to spawnSymbols = GetComponent<SpawnSymbols>();. I'm not entirely sure why this fixed the issue. I think it may be because of the fact that I have multiple "Square" game objects in my scene that have the instantiating script on them and FindObjectOfType (from what I understand) only works if you have one instance of the script that you are using. Please anyone feel free to correct me or chime in if there is something I'm missing here.

EDIT: I ended up using this as my final code: GetComponent<SpawnSymbols>().SpawnPlayerSymbol(); This seems like the most practical since both scripts are attached to the same gameobject.

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

236 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

Related Questions

Script Help for click and auto drop . 0 Answers

How come my scriptable object only receives new information when it's selected in the inspector? 0 Answers

Make ScriptableObject assets customizable. 1 Answer

Reference to scene objects from scriptable objects 1 Answer

ScriptableObject reference resetted after a method is finished. 2 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