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 spirachi77 · Jul 16, 2019 at 03:42 AM · gameobjectinstantiateprefabbugcomponents

prefab is instantiating without a script

I am instantiating a prefab but the attached script on the prefab is not attached during runtime. All other components are on the object but not the script.

the script that instantiates: public class PointerController : MonoBehaviour { public GameObject bullet; private float AngleRad; private float AngleDeg; private Vector3 mPos;

 void Start()
 {
     mPos = new Vector3();
 }
 // Update is called once per frame
 void Update()
 {
     mPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     // Get Angle in Radians
     AngleRad = Mathf.Atan2(this.transform.position.y - mPos.y, this.transform.position.x - mPos.x);
     // Get Angle in Degrees
     AngleDeg = (180 / Mathf.PI) * AngleRad + 90;
     // Rotate Object
     //this.transform.rotation = Quaternion.Euler(0, 0, AngleDeg);
     this.transform.rotation = Quaternion.Lerp(this.transform.rotation, Quaternion.Euler(0, 0, AngleDeg), .7f);

     if (Input.GetMouseButtonDown(0))
     {
         GameObject go = GameObject.Instantiate(bullet, this.transform.position, this.transform.rotation) as GameObject;
     }
 }

}

prefab script:

public class BulletBehavior : MonoBehaviour { public float spd = 10; private Rigidbody2D rb;

 // Start is called before the first frame update
 void Start()
 {
     rb = this.GetComponent<Rigidbody2D>();
 }

 // Update is called once per frame
 void Update()
 {
     rb.velocity = this.transform.forward * spd;
     Debug.Log("here");
 }

 void OnTriggerEnter2D()
 {
     Destroy(this);
 }

}

Comment
Add comment · Show 1
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 Dragate · Jul 16, 2019 at 06:41 AM 1
Share

Probably OnTriggerEnter2D() gets called from the moment your bullet gets instatiated. You should check what the object it collides with is and start your fix from there.

 void OnTriggerEnter2D(Collider2D col){
      Debug.Log(col.gameObject.name);
      Destroy(this); //this destroys the script, not the bullet
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hameed-ullah-jan · Jul 16, 2019 at 07:14 AM

Check your prefab inside "Project" Menu, check if the script is attached there or not? if not then attach this script to you prefab inside "Project" menu.

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 spirachi77 · Jul 16, 2019 at 06:15 PM 0
Share

I checked the prefab itself and it appears to have the script attached to it in there.

avatar image
0

Answer by jayamohanan96 · Jun 07, 2020 at 07:43 PM

Make sure the instantiating script is not a singleton whose instance already exists in the scene. If yes then the newly instantiated object's script will be destroyed as it's instance field is already assigned.

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

200 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

Related Questions

Spawn Prefabs 1 Answer

How to instantiate a prefab with a script attached? 2 Answers

I cant delete multiple instantiated prefabs 0 Answers

Instantiating to gameobject.transform.position 1 Answer

Will Instantiating prefab with many components slow my game? 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