Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 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.

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

ScriptableObject.CreateInstance(Type T) keep crashing 2 Answers

Why is this IEquatable always solving false? 0 Answers

Accessing a specific scripableobject index that can spawn from a single prefab 0 Answers

How costly are those Instantiate and Destroy calls on Scriptable Object? Problem with status effect (buff/debuff) system made by scriptable objects 1 Answer

Scripts don't execute at all. 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