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 /
  • Help Room /
avatar image
1
Question by OctoSloths · Dec 05, 2014 at 08:47 AM · c#2dinstantiateontriggerenter2d

Instantiate object in 2D game?

I have a prefab I'm trying to instantiate when I enter a trigger in a 2D game. I get the following errors:

Assets/Meep/Dialogues/hoardstart.cs(15,37): error CS0103: The name 'hoard' does not exist in the current context.

Assets/Meep/Dialogues/hoardstart.cs(15,44): error CS0119: Expression denotes a type', where a variable', value' or method group' was expected.

Assets/Meep/Dialogues/hoardstart.cs(15,25): error CS1502: The best overloaded method match for 'UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments

Assets/Meep/Dialogues/hoardstart.cs(15,25): error CS1503: Argument #1' cannot convert object' expression to type `UnityEngine.Object'

Here is my script:

 using UnityEngine;
 using System.Collections;
 
 public class hoardstart : MonoBehaviour {
     //private bool bShowGUI = false;
     void Awake(){
         // You must initialize Dialoguer before using it!
         Dialoguer.Initialize();
         
     }
     void OnTriggerEnter2D (Collider2D other){
         if (other.tag == "Player") {
             Dialoguer.StartDialogue (11);
             Destroy(this.gameObject);
             Instantiate(hoard, Vector2 (x, y, 0), Quaternion.identity);
         }
     }
 }
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

3 Replies

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

Answer by ramp · Dec 05, 2014 at 01:14 PM

Hi,

i have resolved all issues,try to use latest updated script.

using UnityEngine;

using System.Collections;

public class hoardstart : MonoBehaviour {

 //private bool bShowGUI = false;

 public GameObject hoard;

 void Awake(){
     // You must initialize Dialoguer before using it!
     Dialoguer.Initialize();
     
 }
 void OnTriggerEnter2D (Collider2D other){
     if (other.tag == "Player") {
         Dialoguer.StartDialogue (11);
         Destroy(this.gameObject);

         Instantiate(hoard,new Vector2 (other.transform.position.x, other.transform.position.y), Quaternion.identity);
     }
 }

}

Thanks

Ram

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 mehtanitish · Nov 28, 2016 at 04:59 AM 0
Share

Thanks, it is really helpful!

avatar image
0

Answer by HarshadK · Dec 05, 2014 at 08:47 AM

There are two things you need to do:

  1. Declare a variable named hoard and assign it the gameobject/prefab you want to instantiate.

  2. Your Vector2 from Instantiate should be Vector3.

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
avatar image
0

Answer by ddjjpp331 · Nov 26, 2016 at 09:09 PM

How would you make the object spawn with the same rotation as the object that spawned it? @ramp

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Involve Instatiated objects in Animation 1 Answer

How to organize an Infinite runner level blocks 1 Answer

Assign GameObject OnMouseUp not through inspector -2D 1 Answer

Script to enable Firing 1 Answer

Size of instantiated clone is different from prefab 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