Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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 CassMeck · Apr 06, 2017 at 12:13 PM · gameobjectprefabscoordinatesreplacehit.point

How do I replace white prefaps with red prefabs?

Hi, I want to do something like below.

alt text

The purpose is to put the red prefaps instead of the white prefeps removed.

But since I use this code (Instantiate(two,hit.point,Quaternion.identity)), where does the mouse click creating a new one there.

How do I replace white prefaps with red prefabs? (Same coordinates)

 for (int x = 0; x < width; x++)
 {for (int z = 0; z < height; z++){
 GameObject gridPlane = (GameObject)Instantiate(one);
 gridPlane.transform.position = new Vector3(gridPlane.transform.position.x + x,
 gridPlane.transform.position.y, gridPlane.transform.position.z + z);
 grid[x,z] = gridPlane;}}

and,

 if(Input.GetMouseButtonDown(0)){
 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 RaycastHit hit;
 if (Physics.Raycast (ray, out hit)) {
 Destroy (hit.transform.gameObject);
 Instantiate(two,hit.point,Quaternion.identity);}}

but.png (132.5 kB)
Comment
Add comment · Show 4
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 PizzaPie · Apr 06, 2017 at 03:05 PM 0
Share

Why not just change the color in the existing ones?

avatar image CassMeck PizzaPie · Apr 06, 2017 at 03:23 PM 0
Share

I'm printing the total number of boxes.I can see how long you have stayed when the number of boxes is getting low. (red or white box) So I wanted to do it using a prefaps.If the color changes, can I say I have one in this color? For example, white box total count = 2 red box total count = 0

When I click on the box, white box total count = 1 (-1) red box total count = 1 (+1)

avatar image PizzaPie CassMeck · Apr 06, 2017 at 03:37 PM 0
Share

You could use an enum or bool that represents current state and you change it when you change the actual color.After that create a class tha controls the counts and with a reference to it in the box class add or subtract accordingly when changes occure. From performance aspect that is better than constant instatiating and destorying.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by KKS21199 · Apr 06, 2017 at 05:35 PM

Hey,

You can just Instantiate the red prefab before destroying the white.

 if(Input.GetMouseButtonDown(0)){
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast (ray, out hit)) {
                 Instantiate(two,hit.transform.position,hit.transform.rotation);}}
                Destroy (hit.transform.gameObject);
         }
   }
 

Edit: Though I suggest using color change which could give better performance as @PizzaPie suggested.

You can count the total number of white box and red box by using a manager script attached to an empty gameObject.

So an example of what the manager script should have.

     public int whiteCount = 0;
     public int redCount = 0;

and within your whitebox or redbox script call

 whiteCount += 1;
 redCount -= 1;

when you are changing the colors.

To change the material colors, given that you only need two materials, you don't need a list.

Just add the following to your gameobject script

 public Material redMaterial;
 public Material whiteMaterial;

and when you know that you want to change the material

 hit.transform.gameObject.GetComponent<Renderer>().material = redMaterial;
Comment
Add comment · Show 2 · 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
avatar image CassMeck · Apr 06, 2017 at 07:25 PM 0
Share

hit.transform.position,hit.transform.rotation hit.transform.gameObject.GetComponent().material = red$$anonymous$$aterial;

The first code is doing the work I want to do.So In the second code, the color change succeeded. Problem, the counts as +1 when clicked with the mouse again on the red square. To prevent this, I added a bool.But just clicking with the mouse does not allow you to click on the entire field, not the square.

But, @PizzaPie and as you said, I will not compare two methods in terms of performance.

I'll work on it a little more.

avatar image KKS21199 CassMeck · Apr 07, 2017 at 09:41 AM 0
Share

Accept the answer if it works. You can do a simple if check to make sure the count doesnt count if double clicking on a red tile.

 if( hit.transform.gameObject.GetComponent<Renderer>().material == white$$anonymous$$aterial)
 {
 redcount ++;
 whitecount --;
  hit.transform.gameObject.GetComponent<Renderer>().material = red$$anonymous$$aterial;
 }

just make sure to set the white$$anonymous$$aterial when instantiating each of the white prefab so the reference is set right.

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

119 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

Related Questions

How to handle levels 0 Answers

Unity Addressable for player skins 0 Answers

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

connect 3 gameobjects 0 Answers

How can I update the gameobjects in the scene for which i replaced the prefab in project window? 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