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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Lakistein · Nov 17, 2013 at 05:44 PM · unity 4.3class objectanother scriptprefabe

NullReference

Hello everyone, I'm new to unity 4.3 and I have a problem. So, I've created a prefab which contains a script Spawner.

 public GameObject sheepToSpawn;
 public GameObject powerUpToSpawn;

 Sheep sheep;
 //PowerUps powerUps;
 
 void Awake()
 {
     sheep = GetComponent<Sheep>();
     //powerUps = GetComponent<PowerUps>();
 }

 void Update()
 {
     sheep.SpawnSheep(sheepToSpawn);
     //powerUps.SpawnPowerUp(powerUpToSpawn);

 }

In the inspector, I put another prefab, which contains script Sheep, into sheepToSpawn field of the first prefab. Sheep class:

 public void SpawnSheep(GameObject sheepToSpawn)
 {
     for (int i = 0; i <= Random.Range(1, 5); i++)
     {

         listOfSheeps.Add((GameObject)Instantiate(sheepToSpawn, new Vector3(Random.Range(-7.0f, 7.0f), Random.Range(-4.0f, 4.0f), 0), transform.rotation));

     }  
 }

alt text

I'm also getting this error : NullReferenceException: Object reference not set to an instance of an object Spawner.Update () (at Assets/Scripts/Spawner.cs:26)

If someone can help me with this problem and edit code if possible, I would be grateful. Thanks in advance :)

untitled.jpg (93.6 kB)
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
1

Answer by FunctionR · Nov 17, 2013 at 09:03 PM

Your problem is here:

 void Awake()
 {
     sheep = GetComponent<Sheep>();
 }

sheep is actually being set to null, because GetComponent() is not finding your Sheep script. It can't find it because you are looking for the script inside the Spawner GameObject.

Solution

  1. Identify which GameObject has the Sheep script.

  2. Get a pointer to that GameObject.

  3. Use the pointer to find the Sheep script inside that GameObject.

Edit because of the code you posted below:

 public GameObject sheepToSpawn;

Since sheepToSpawn points to the prefab, you have most of your problem solved already!

In the Awake() function you can instantiate the prefab. Like this:

 void Awake()
 {
    GameObject sheepObj = Instantiate(sheepToSpawn) as GameObject;

    sheep = sheepObj.GetComponent<Sheep>();
 }

Now sheep points to the script in the instantiated GameObject.

Comment
Add comment · Show 2 · 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 Lakistein · Nov 17, 2013 at 11:10 PM 0
Share

It works when there is already game object in the scene. In my case, game object is not instantiated yet.

This is new code, but it doesn't work void Awake() { sheep = GameObject.Find("Sheep").GetComponent<Sheep>(); } So, i need to access component of Sheep prefab which is not instantiated. btw, thx for response I really appreciate that :)

avatar image FunctionR · Nov 18, 2013 at 04:30 AM 0
Share

No problem. You can instantiate your prefab. Look at my Edit above, that should solve your problem.

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

18 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

Related Questions

Changing a variable in another script for one object. 2 Answers

How to change texture of another object by clicking on a object? 0 Answers

detecting whether the object has been clicked in another script 2 Answers

NullReferenceException: Object reference not set to an instance of an object spawn.Update () (at Assets/spawn.cs:24) 1 Answer

i have a String with the VALUE "abc" and want to set a bool with the NAME "abc" to true based on the string value 2 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