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 RoyaleNite · Mar 07, 2019 at 03:22 PM · prefab-instance

Edit only one of many instantiated objects

So I'm instantiating cubes with rigid bodies every 1.5 seconds at random locations. But when the instantiated cubes hit the platform their isKinematic is turn to true so they don't move anymore. But when I set it to true every instantiated cubes isKinematic is set to true when they get instantiated.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Bloackfall : MonoBehaviour
 {
     public Rigidbody fallingBlockPrefab;
     public static Rigidbody fallingBlockInstance;
 
     // Start is called before the first frame update
     void Start()
     {
         StartCoroutine("SpawningBlocks");
 
     }
     // Update is called once per frame
     void Update()
     {
 
     }
 
     IEnumerator SpawningBlocks()
     {
 
         yield return new WaitForSeconds(SpawnTime);
         int Randx, randZ;
         Randx = Random.RandomRange(0, 10);
         randZ = Random.RandomRange(0, 10);
 
         fallingBlockInstance = Instantiate(fallingBlockPrefab, new Vector3(Randx, 20, randZ), transform.rotation); 
 
         StartCoroutine("SpawningBlocks");
 
     }

and the OnCollision script is on the prefab itself

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class onCollision : MonoBehaviour
 {
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         
         if (collision.gameObject.tag == "Platform")
         {
             Bloackfall.fallingBlockInstance.isKinematic = true;
         }
 
     }
 
 }

 
Comment
Add comment · Show 3
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 xxmariofer · Mar 07, 2019 at 03:35 PM 0
Share

remove the static from the fallingblockinstance

avatar image RoyaleNite xxmariofer · Mar 07, 2019 at 05:35 PM 0
Share

if I remove the static I wont be able to use the on collision enter in the other script

avatar image RoyaleNite xxmariofer · Mar 07, 2019 at 05:41 PM 0
Share

Also didn't fix the problem even if I removed the static

1 Reply

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

Answer by mchts · Mar 07, 2019 at 05:57 PM

Instantiate() as the name implies means creating an "individual" instance of an object. If you want to handle each instance individually just attach your onCollision script to your prefab and change collision detection part like:

 private void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject.tag == "Platform") {
              this.gameObject.GetComponent<Rigidbody>().isKinematic = true;
     }
  }

Btw you won't need public static Rigidbody fallingBlockInstance; field anymore unless you want to keep a reference to your instance. If you want so you should keep track of every instance in a different way e.g. pooling or keeping them in a container and its a complete different story.

Comment
Add comment · Show 1 · 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 RoyaleNite · Mar 07, 2019 at 07:14 PM 0
Share

Thank you very much, much simpler than what I was trying to do. I'll have to do some research on containers

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

102 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

Related Questions

Source Gameobject rendered when using prefab 0 Answers

Custom Inspector: Using SeralizedProperty changes the prefab values! 0 Answers

Instantiating prefabs creates a clone, not instance of the prefab 1 Answer

"Unable to instantiate prefab. Prefab may be broken" 7 Answers

Script is reading from prefab instead of instance 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