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 coryl · Jul 01, 2011 at 08:02 AM · arrayvariablevariables

Create new variable name based on array index

Hello friends,

I am currently storing a bunch of Vector3 positions in an array. When a user clicks the screen, I am using a Raycast to get the location, sticking it in the array, and then instantiating an object at that location.

I would like to access the properties of those instantiations (ie. their rotations) for other game related calculations. However, my function currently does not create a new variable for each instantiation, so I can't access the GameObjects.

I was thinking something like this would make sense in my function:

 var gameObject[positions.Count] = Instantiate(marker, .., ..);

(actually using a List, hack because of Vector3 array complications). This would hopefully result in something like "gameObject1", "gameObject2", etc. How could I append the index number (or count in this case) to the variable name?

Thanks

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
1
Best Answer

Answer by GuyTidhar · Jul 01, 2011 at 09:34 AM

Have you got an "else" there? Don't insert the reference if it is null - that's why I put an else before doing the "Add" to the array.

Do you know coroutines? You can have all this code inside a coroutine and wait a frame before checking for the object initialization.

For instance:

 function CallingFunction()
 {
   StartCoroutine("AddMarker");
 }

 function AddMarker()
 {
    var tempObj : GameObject = Instantiate(marker, selected.point, transform.rotation) as GameObject;
 
    yield;
 
    if(!tempObj)
    {
       print("failed to instantiate tempobj");
    }
    else
    {
      gameObjects.Add(tempObj); 
      print(gameObjects.Count);
    }
 }
Comment
Add comment · Show 4 · 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 coryl · Jul 01, 2011 at 09:48 AM 0
Share

Hey guyt,

I did miss the else{}, and added it. I also moved my code into the functions like yours, and I'm running CallingFunction() on the mouse click. Everything is working fine in the game screen, however tempObj just isn't getting "instantiated" despite showing up on the screen.

Thanks for your help btw, much appreciated. So I guess the problem lies in tempObj?

avatar image GuyTidhar · Jul 01, 2011 at 09:59 AM 0
Share

Did you use Add$$anonymous$$arker function as I wrote it and called it with StartCoroutine?

try change the line where it says: "yield;" to "yield WaitForSeconds(3);" And see if after 3 seconds from calling this function, whether the print shows correctly.

Are you sure the object "marker" is actually created in the scene? Is it really a GameObject? $$anonymous$$ight it something else? If it is not a GameOject, the tempObj will always be null in this case.

avatar image coryl · Jul 01, 2011 at 10:12 AM 0
Share

Yeehaw! Yes, marker was originally set as a marker : Transform in the beginning when I loaded it with the inspector. So I guess despite trying to Instantiate(marker) as GameObject, it was still not a GameObject. I simply changed "var marker : GameObject" and everything lined up with your code.

Thanks for your help, I can now access their positions and rotations. :D

avatar image GuyTidhar · Jul 01, 2011 at 10:21 AM 0
Share

Ooh..Great! Glad I helped eventually :)

avatar image
1

Answer by GuyTidhar · Jul 01, 2011 at 08:19 AM

Did you initialize the array of GameObjects? Is it an array?

If it is an constant size array, you should do:

 var gameObjects : GameObject[Max_Number_Of_GameObjects];

If it is not constant, you need to use Array or List for the game objects too.

 var gameObjects = List.<GameObject>();

And then Add GameObject as you instantiate them

Comment
Add comment · Show 4 · 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 coryl · Jul 01, 2011 at 08:40 AM 0
Share

Hey guyt, so I followed your advice about storing my instantiated devices in an array. However, I just realized that Instantiated objects aren't game objects, they're just "objects"? Objects don't have access to the transform class, so I can't get position or rotation. I cannot convert Objects to Game Objects, or at least I'm not sure how.

Otherwise I am now storing these objects in a list. Thanks

avatar image GuyTidhar · Jul 01, 2011 at 08:48 AM 0
Share

Do: gameObjects.Add(Instantiate(marker, .., ..) as GameObject);

And: gameObjects[index] as GameObject;

GameObject is based on Object. When one class is based on another, you can tell the base to be regarded by the program as the second one using the word "as" and setting it into a variable.

avatar image coryl · Jul 01, 2011 at 09:04 AM 0
Share

Great! So I'm using: gameObjects.Add(Instantiate(marker, .., ..) as GameObject); which seems to be adding the object to the list properly. At least, the list.Count is going up.

However, when I run a test to check the position right after that line: print(gameObjects[i].transform.position);

I receive an error: NullReferenceException: Object reference not set to an instance of an object

Any ideas? Thanks

*EDIT: I just realized my [i] value is meaningless. So I set it to gameObjects[0].transform.position which should retrieve the position of the first object. However, its still returning that same error of nullreferenceexception.

avatar image GuyTidhar · Jul 01, 2011 at 09:10 AM 0
Share

I'll open a new answer to list some code.

avatar image
1

Answer by GuyTidhar · Jul 01, 2011 at 09:16 AM

Generally, you should make sure a value you initialize (in this case - a GameObject you instantiate), did so successfully. I'd do something like this:

 // Initialize memory
 var gameObjects : Array = new Array();
 var newGameObject : GameObject = Instantiate(marker, .., ..) as GameObject;
 if ( !newGameObject )
    Debug.LogError("Could not initialize a newGameObject!");
 else // Now you know a GameObject was actually created
 {
    // Add the GameObject to the array
    gameObjects.Add(newGameObject);
    // Output the position of the last GameObject added (Coount - 1)
    Debug.Log("Position of GameObject: " + (gameObjects[gameObjects.Count-1] as GameObject).transform.position);
 }
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 coryl · Jul 01, 2011 at 09:25 AM 0
Share

Okay, odd situation.

var tempObj : GameObject = Instantiate(marker, selected.point, transform.rotation) as GameObject;

if(!tempObj){ print("failed to instantiate tempobj"); }

gameObjects.Add(tempObj); print(gameObjects.Count);

So the debug is actually telling me that tempObj is failing to instantiate. However, I see the market appearing right on the screen, so I'm not sure whats going on there. GameObjects.Count is also incrementing fine, but I guess nothing is actually being inserted into the list.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Problem when trying to save variable in another variable. 1 Answer

Array with boolean variables? 3 Answers

Transfering Variables Between Sections [SOLVED] 2 Answers

Custom editor window resets array 1 Answer

Accessing itween variables 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