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 /
avatar image
0
Question by Duncan · Nov 06, 2010 at 12:13 PM · arrayvector3bce0048

Vector 3 in array

hi

i'm trying to use an array containing three Vector3's which refer to 3 respawning locations.

when the player dies (or in the code, has a transform.x >80) then new respawn position is randomly chosen from the array.

function Start () { var array = new Array (Vector3(0, 5, 70), Vector3(-50, 5, -50), Vector3(60, 5, -20)); }

function Update () { var randomposition = Random.Range (0, 2); // select random number var newlocation: Vector3[] = array[randomposition]; //new Vector 3 from array

if(transform.position.x >80) // player moves past arbitary point, new location { transform.position = newlocation; } }

however i get the error message

BCE0048: Type 'System.Type' does not support slicing.

i'm fairly new to Unity so apologies if i'm making a fundamental mistake here!

cheers duncan

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Peter G · Nov 06, 2010 at 12:34 PM

I'm going to give you 2 codes, the first, since you are new to this will explain what you did wrong or could do better, and the second will be a functional version to help you out.

function Start () { var array = new Array (Vector3(0, 5, 70), Vector3(-50, 5, -50), Vector3(60, 5, -20)); //You can make an array like this, but they are slower for several reasons. //.Net arrays , like you did below, are a better choice.

  //And, you created this array locally, so when you access it in your
  //other methods, it does not exist.

}

function Update () { var randomposition : int = Random.Range (0, 2); // select random number var newlocation: Vector3[] = array[randomposition]; //new Vector 3 from array // This is the line with the error. You are creating a new Vector3[] but are assigning it the value of a Vector3.

  if(transform.position.x >80) // player moves past arbitary point, new location 
  //You should also not hardcode values.
  {
      transform.position = newlocation;
      //This is fine except you assigned it the wrong type above.    
  }

}

Here is the corrected code.

var spawnPoints : Vector3[]; //create an array of positions var minDistForRand : float = 80; //Add both these values in the inspector.

function Update () { var randomIndex : int = Random.Range(0, spawnPoints.Length); //Choose a random number based on the length of the array

  var newLocation : Vector3 = spawnPoints[randomIndex];
  //Choose a new point from the array of spawn points.

  if(transform.position.x > minDistForRand) {

        transform.position = newLocation;
  }

}

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 Eric5h5 · Nov 06, 2010 at 04:48 PM 0
Share

Random.Range should use spawnPoints.Length, not Length - 1. See the docs for Random.Range with integers. Also it should be just "`var spawnPoints : Vector3[];`"

avatar image Peter G · Nov 06, 2010 at 04:58 PM 0
Share

Oops, thanks for the correction.

avatar image Duncan · Nov 07, 2010 at 11:48 AM 0
Share

Thanks, that's exactly what i wanted. cheers

avatar image ina · Apr 23, 2011 at 06:06 PM 0
Share

Array.Push does not work for Vector3[]

avatar image Peter G · Apr 23, 2011 at 06:19 PM 0
Share

Ummm, ok i guess. Where did you see Array.Push?

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

No one has followed this question yet.

Related Questions

Vector1, Vector2, Vector3? 1 Answer

Rotate Vector3 array around a point 1 Answer

Javascript - find a value in an array 0 Answers

Multiple Fire Points 2 Answers

Get size of a vector3 array c# 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