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 /
  • Help Room /
avatar image
0
Question by zak666 · Feb 20, 2018 at 08:11 PM · instantiatevector3scriptingbasics

Instantiate Object using Vector3 at posision?

hey gang Made a simple scriptwere when you Kill a Bunch of enemies and when their are no enemies in the group it finds the last Vector3 of the last enemy and then Instantiates and Item, Just wondering how you use a Vector3 as the posisoin rather then using Transform.posision, ?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SquadrinKill : MonoBehaviour {
 
     public GameObject[] Squadrin;
     public GameObject Item;
     public Vector3 ItemSpawnpoint;
 
 
     
     // Update is called once per frame
     void Update () {
 
             // Get Last posision of Lsat Squadrin Member
         if (Squadrin.Length == 1) {
             ItemSpawnpoint = Squadrin [1].transform.position;
         }
 
         if (Squadrin == null) {
             GameObject Clone = Instantiate (Item, ItemSpawnpoint);
         }
     }
 }
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 Bonfire-Boy · Feb 21, 2018 at 02:57 PM 1
Share

Transform.position is a Vector3, so the question about "using a Vector2 rather than Transform.position" doesn't really make sense. In any case, what you're doing with respect to those things looks basically correct.

There are two glaring problems with the code though...

1) If Squadrin.Length is 1, then it doesn't have an element with index 1 (because they start at zero). Therefore if line 18 is reached, it is guaranteed to throw an index out of bounds exception.

2) At the end you check if Squadrin is null, but you tried to access Squadrin.Length in the previous "if" statement. So if Squadrin were null, it would have thrown a null reference exception before it even got to your null test.

1 Reply

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

Answer by hugodeheld · Feb 21, 2018 at 12:00 PM

I think this should do it:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class SquadrinKill : MonoBehaviour {
  
      public List <GameObject> Squadrin;
      public GameObject Item;
      public Vector3 ItemSpawnpoint;
  
      
      // Update is called once per frame
      void Update () {
          
          for(int i = 0; i < Squadrin.Count; i++){
              if(Squadrin[i] == null){
                  Squadrin.RemoveAt(i)
                  i--;
              }
          
          }
          if (Squadrin.Count == 1) {
              Debug.Log ("PRINT pOSISION FOR iTEM" + ItemSpawnpoint); // Not Being Printed
              ItemSpawnpoint = Squadrin[0].transform.position;
          }
  
              // Get Last posision of Lsat Squadrin Member
          if (Squadrin.Count == 0) {
              GameObject Clone = Instantiate (Item, ItemSpawnpoint, Quaternion.identity);  // Not Instaniating
              Destroy (this);
          }
      }
  }
 

Instantiate takes 3 arguments if you want to create an object at a position. Also you should destory the object after otherwise it wil keep spawning the object

Comment
Add comment · Show 5 · 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 dmitriy-untilov · Feb 21, 2018 at 01:05 PM 1
Share

In line with code ItemSpawnpoint = Squadrin [1].transform.position; there is an error. You are checking Count with 1, but numeration in array performs from 0, so in this case you`ll be trying to access 2nd array element. Change to Squadrin [0] or Squadrin.First()

avatar image hugodeheld dmitriy-untilov · Feb 21, 2018 at 02:05 PM 0
Share

Thanks, didnt read the code that much

avatar image zak666 hugodeheld · Feb 21, 2018 at 10:27 PM 0
Share

Thanks for the replies guys, cleared some things up, howerever after setting everything up the Item dose not instantiate in the game and has been assigned in the inspector :)

alt text

item.png (13.3 kB)
Show more comments

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

115 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

Related Questions

Instantiate ignored Vector3 0 Answers

[C#] Object not spawning where it should? 0 Answers

[SOLVED] Remove Vector3 Array step by step when GameObject spawned 1 Answer

Instantiate() Argument error 1 Answer

i get error when i add vector 3 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