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 IBDelta · Oct 07, 2010 at 09:36 AM · instantiatescriptingbasics

Simple clone scripting

I'm a complete noob just pootling along and trying to make a fish-tank with an ecosystem for fun. I want to start the base of the food chain with plankton but I'm having difficulty, as soon as I try to add a variable to the instantiated prefab I get a null object reference.

There are two scripts, the first is used to generate the plankton;

    var Plankton_Archae_Prefab:GameObject;
function Start () {
for (var i : int = 0;i < 100; i++) {
    var position = Vector3(Random.Range(0, 500),Random.Range(100, 300), Random.Range(0, 500));
    Instantiate(Plankton_Archae_Prefab, position, Quaternion.identity);
    }
}

The second script is attached to an individual plankton, this is the one that throws a wobbly;

var Plankton_Archae_Prefab:GameObject;

var Currentage; //Current Age of Plankton var Maxage; //Maximum Age of Plankton var hp; //Hit Points var div; //Number of cell divisions

var speed = 5; //Movement speed

var PlankEnergy; //Energy of plankton

function Update () { PlankEnergy++; rigidbody.position = Vector3.Lerp(rigidbody.position,rigidbody.position + Vector3((Random.value-0.5)*speed,(Random.value-0.5)*speed,(Random.value-0.5)*speed),Time.time); if(PlankEnergy==10){ Instantiate(Plankton_Archae_Prefab, rigidbody.position, Quaternion.identity); } }

I've tried moving the PlankEnergy variable into the update function, making it a private variable and all sorts. If I remove the PlankEnergy variable then the script works fine, problem is I need that variable :-)

Note: Although it's designed for plankton the basic script is really a study in bacteria.

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 · Oct 07, 2010 at 09:55 AM 0
Share

This may sound silly, but have you assigned the value of Plankton_Archae_Prefab on the script attached to the plankton?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by I B Delta 1 · Oct 07, 2010 at 10:47 AM

I think this is what you mean;

var Plankton_Archae_Prefab:GameObject;

var Currentage; //Current Age of Plankton var Maxage; //Maximum Age of Plankton var hp; //Hit Points var div; //Number of cell divisions

var speed = 5; //Movement speed

var PlankEnergy; //Energy of plankton

function Start (){ //Just added PlankEnergy==0; // Sets starting value }

function Update () { PlankEnergy++; rigidbody.position = Vector3.Lerp(rigidbody.position,rigidbody.position + Vector3((Random.value-0.5)*speed,(Random.value-0.5)*speed,(Random.value-0.5)*speed),Time.time); if(PlankEnergy==10){ Instantiate(Plankton_Archae_Prefab, rigidbody.position, Quaternion.identity); } }

It still failed to work :-( All of the links in the inspector have been triple checked and all are in place. It appears to be trying to access the wrong script when it runs.

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

1 Person is following this question.

avatar image

Related Questions

Instatiating a prefab in a random position 0 Answers

Moving with instantiated projectile 2 Answers

Instantiate script then edit its variables 2 Answers

Script that switches between first and third person controller 3 Answers

Clones with individual scripts 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