Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 rahulpatil6220375 · May 02, 2019 at 09:34 AM · instantiatecube

Problem with repeating objects in array when already in the array

When the player move forward then paint is instantiate and stored in the array, even if the paint is already in the array it will again be stored in the array.

But I don't want to repeat any paint in the array.

 public GameObject[] array;
 public GameObject paint;
 int temp = 0;
 private RaycastHit hit;
 
 public float speed = 10f;                             
 public Text levelText;
 public GameObject paint;
 float maxdistance = 0.51f; 
 Vector3 newtargetposition;
 bool pos;
 
 private void FixedUpdate()
 {
 
  if(Physics.Raycast(transform.position,transform.TransformDirection(Vector3.forw ard,out hit, maxdistance))
  {
     if (hit.collider.gameObject.tag == "Obstacle")
     {
             move = false;                                                                         
     }
 
     if (move)
     {
         PlayerMove();                         
     }
  }
 }
 
 public void PlayerMove()
 {
     if (move)
     {
       vector3 positions = transform.position + new Vector3(0f, -0.5f,0f); //when player move then paint instantiate y position 
 
         array[temp] = Instantiate<GameObject>(paint, position, Quaternion.identity);
         temp++;
 
         newtargetposition=position;
         Debug.Log("newtargetposiiton:" + newtargetposition);
 
         if (temp == 150)    
         {
             if (newtargetposition == position)
             {
                 //what can i do here
                 //i dont want to paint here because position store in newtargetposition,it is repeat
                 //array[temp]--;
                 Debug.Log("newtargetpositions:" + newtargetposition);
                 //temp--;
             }
             gameOver.SetActive(true);
             SceneManager.LoadScene(1);
         }
     }
 }

when player move forward alt text

when player move backword

alt text

How can I remove repeated instantiation of the object and repeated objects in the array?

backmove.png (190.9 kB)
playerforward.png (184.4 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 mchts · May 02, 2019 at 10:28 AM 0
Share

Use HashSet to store positions. HasSet provides uniqeness. It won't add same Vector3 value into collection.

avatar image tormentoarmagedoom · May 02, 2019 at 10:53 AM 0
Share

Why not before instantiate, just check if it already exists?

if (!exists) Instantiate();

Bye :D

avatar image rahulpatil6220375 tormentoarmagedoom · May 02, 2019 at 01:58 PM 0
Share

can u edit my code????plz...

avatar image mchts · May 03, 2019 at 06:47 AM 0
Share
 public GameObject[] array;
 public GameObject paint;
 int temp = 0;
 private RaycastHit hit;
 
 public float speed = 10f;                             
 public Text levelText;
 public GameObject paint;
 float maxdistance = 0.51f; 
 Vector3 newtargetposition;
 bool pos;
 
 HashSet<Vector3> paintPositions;
 
 private void Awake(){
     paintPositions = new HashSet<Vector3>();
 }
 
 private void FixedUpdate() {
     if(Physics.Raycast(transform.position,transform.TransformDirection(Vector3.forw ard,out hit, maxdistance))
     {
         if (hit.collider.gameObject.tag == "Obstacle")
         {
             move = false;                                                                         
         } 
         if (move)
         {
             Player$$anonymous$$ove();                         
         }
     }
 }
 
 public void Player$$anonymous$$ove()
 {
     if (move)
     {
         vector3 positions = transform.position + new Vector3(0f, -0.5f,0f); //when player move then paint instantiate y position 
         
         if(!paintPositions.Contains(positions)) {//if position doesn't already exist  
             array[temp] = Instantiate<GameObject>(paint, position, Quaternion.identity);
             temp++;
             newtargetposition=position;
             Debug.Log("newtargetposiiton:" + newtargetposition);
         }
 
         if (temp == 150)    
         {
             if (newtargetposition == position)
             {
                 //what can i do here
                 //i dont want to paint here because position store in newtargetposition,it is repeat
                 //array[temp]--;
                 Debug.Log("newtargetpositions:" + newtargetposition);
                 //temp--;
             }
             gameOver.SetActive(true);
             Scene$$anonymous$$anager.LoadScene(1);
         }
     }
 }

I just added the mechanism to control instantiate phase. Your code needs to be revised though.

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

127 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

Related Questions

Instantiating prefabs, when there is nothing 2 Answers

Adding Imperfections? 1 Answer

(C#) How does one position instantiated objects locally, not to the world? 1 Answer

Placing static prefabs on click 2 Answers

instantiate works on one but not other object 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