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 OtsegoDoom · Oct 18, 2011 at 09:46 PM · instantiatenullreferenceexception

Instantiate Object with Specific Values

Hi Everyone,

I've been trying for a while now to figure this out but I'm stumped. I'm trying to instantiate a group of objects each with random values for certain properties (Ex. larger scale, faster move speed, etc). This script works for the most part but the values only get increased on the original object, not any of the instances. If I run this script as is, I get a NullReferenceExpection error. The issue is how to access the variables in instanced objects individually, rather than globally.

 //Variables for Minimun and Maximum emit rates
 static var emitRateMin : float = 1.0;
 static var emitRateMax : float = 2.0;

 var enemyCube : GameObject;

 function Start () {

 while (true){

The "moveForward" script is attached to every enemy object, this script controls the speed the object moves at. It will eventually be updated to include health, armor and scale. All of which should be randomly generated. The moveForward script contains a variable called moveSpeed.

     var script : moveForward;

     yield WaitForSeconds(Random.Range(emitRateMin,emitRateMax));

     var newEnemy = Instantiate (enemyCube, transform.TransformPoint(Vector3(Random.Range(-9, 9), 0, 0)), Quaternion.Euler(0, Random.Range(80, 100), 0));
 

This line is causing the problem I think. Instead of modifying the moveSpeed variable of each instance, it modifies the variable of the original object only. This value keeps going up and up with each new object instantiated. The instances are unchanged.

         script.moveSpeed += Random.Range(-0.5,2);
 
     }
 
 }

Full script without comments:

     static var emitRateMin : float = 1.0;
     static var emitRateMax : float = 2.0;
 
     var enemyCube : GameObject;
 
     function Start () {
 
     while (true){

         var script : moveForward;
 
         yield WaitForSeconds(Random.Range(emitRateMin,emitRateMax));
 
         var newEnemy = Instantiate (enemyCube, transform.TransformPoint(Vector3(Random.Range(-9, 9), 0, 0)), Quaternion.Euler(0, Random.Range(80, 100), 0));
 
             script.moveSpeed += Random.Range(-0.5,2);
     
         }
     
     }
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

2 Replies

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

Answer by DaveA · Oct 18, 2011 at 11:49 PM

Something like this:

 var newEnemy = Instantiate (enemyCube, transform.TransformPoint(Vector3(Random.Range(-9, 9), 0, 0)), Quaternion.Euler(0, Random.Range(80, 100), 0));
 script = newEnemy.GetComponent(moveForward);
 script.moveSpeed += Random.Range(-0.5,2);
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 OtsegoDoom · Oct 19, 2011 at 01:27 AM 0
Share

That worked perfectly, and the best part is I understand what's going on. Thank you!

avatar image zv_odd · Mar 30, 2012 at 08:28 AM 0
Share

This doesn't work. I get the error : 'GetComponent' is not a member of UnityEngine.Object

So how did this ever work - if 'Instantiate' returns a Object not a GameObject!?

avatar image Michael_Berna zv_odd · Aug 08, 2019 at 05:28 AM 0
Share

Are you using c#? If so. here's my c# equivalent based on this answer that worked for me.

 GameObject dropped$$anonymous$$oneyBag = Instantiate(moneyBag, transform.position, transform.rotation);//drop gold
         short randomGoldDropped = (Int16)UnityEngine.Random.Range($$anonymous$$CoinsDropped, maxCoinsDropped + 1);
         dropped$$anonymous$$oneyBag.GetComponent<CoinBagScript>().goldAmmount = randomGoldDropped;//set how much gold is dropped

avatar image Meishin zv_odd · Aug 08, 2019 at 07:02 AM 0
Share

@zv_odd, you can cast;

GameObject newGameObject = Instantiate(whatever) as GameObject;

or

GameObject newGameObject = (GameObject) Instantiate(whatever)

avatar image
0

Answer by sacredgeometry · Aug 10, 2019 at 09:28 AM

"This line is causing the problem I think. Instead of modifying the moveSpeed variable of each instance, it modifies the variable of the original object only. This value keeps going up and up with each new object instantiated. The instances are unchanged."

Is it static? Static variables are class members not instance members. Meaning there is only one shared variable for the whole class, when you set the one variable it updates for anywhere that reads it. They are not like an instance member where the instance gets its own memory. Static means that it belongs to the class. It can cause all sorts of concurrency issues so I would just avoid it.

As a general rule you should rarely if ever use static. It's mostly bad practice and code smell and there are very few actual legitimate uses for it.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Variable Assigning with GameObject.Find("") causing NullReferenceException? 0 Answers

Simultaneous Null Reference Exception and expected value 1 Answer

Accessing a prefab after instantiating results as null 1 Answer

Networking-multplayer issue 1 Answer

Unity iOS Javascript - Problem with Instantiate 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