Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
2
Question by animeman0 · Sep 02, 2015 at 02:07 PM · c#gameobjectprefabunassiunassignedreferenceexception

UnassignedReferenceException: The variable has not been assigned, even though it has. - c#

I have a script that controls shooting and it's assigned to a prefab. I hit play to see if it works and it says this when I shoot:

UnassignedReferenceException: The variable player of BulletController has not been assigned. You probably need to assign the player variable of the BulletController script in the inspector. UnityEngine.GameObject.GetComponent[PlayerController] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineGameObjectBindings.gen.cs:35) BulletController.Start () (at Assets/Scripts/BulletController.cs:22)

However as you can see I've assigned my player variable:

alt text

I did click apply as well but nothing happened. I tried clicking on the cog icon and clicking reset but it still didn't work.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class BulletController : MonoBehaviour {
 
 
     PlayerController PlayerControllerScript;
 
     public float speed;
     public float coolDown = 5f;
     public float coolDownTimer;
 
     public Transform bullet;
 
     public GameObject player;
     //Rigidbody2D rb;
 
     
     void Start()
     {
         PlayerControllerScript = player.GetComponent<PlayerController> ();
     }
     void Update()
     {
         Firing ();
         bulletActive ();
     }
 
     void Firing()
     {
         bullet.position += bullet.up * Time.deltaTime * speed;
     }
 
 
     public void OnTriggerEnter2D(Collider2D other)
     {
         if(other.CompareTag("Enemy"))
         {
             Destroy(other.gameObject);
             PlayerControllerScript.score += 10;
             PlayerControllerScript.ScoreText();
         }
         Destroy (gameObject);
     }

Thanks in advance.

assigned-player.png (40.8 kB)
Comment
Add comment · Show 6
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 Landern · Sep 02, 2015 at 02:16 PM 0
Share

You should update with the relevant portions of your script, so say lines 1-40(for padding)

avatar image animeman0 · Sep 02, 2015 at 02:33 PM 0
Share

what do you mean by update? @Landern

avatar image Dave-Carlile · Sep 02, 2015 at 03:13 PM 0
Share

Include your script in your question.

avatar image animeman0 · Sep 02, 2015 at 03:34 PM 0
Share

I added script to show how I'm using it. I I used the inspector to assign the variable the line of code I used to do it was:

 public GameObject player;
avatar image Dave-Carlile · Sep 02, 2015 at 06:02 PM 0
Share

Do you have this script on another object maybe? where the player isn't assigned?

Add a Debug.Log call to Start, see if you get more than one log message. That has tripped me up an embarrassing number of times.

Show more comments

3 Replies

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

Answer by BlackSnow_2 · Sep 02, 2015 at 11:36 PM

Your issue is probably the issue I was having...

Can you see that the object field is bolded? This means it's differing from the prefab of the same name. You've probably assigned an object in the scene to the variable, which it can't save into a prefab.

Now, there's two courses of action you can take here: 1. Turn the player into a prefab and use that prefab on the projectile's inspector Player variable (But this wastes space in your assets, so I prefer 2) 2. Use something like this:

 public GameObject Player_Blue;
 
     void Start() 
     {
         Player_Blue = GameObject.FindWithTag ("Player");
     }


Then make sure your player object has the "Player" tag and you should be good to go!

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 animeman0 · Sep 04, 2015 at 01:44 PM 0
Share

Thanks answer number 2 was perfect!

avatar image
0

Answer by Camikazee · Mar 09, 2017 at 09:00 PM

I had this issue after renaming a script, the debugger showed everything as assigned but I got an exception on Instantiate. I had to recreate the prefabs in the end which wasn't great.

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
avatar image
0

Answer by unity_hqhvp8Zt6unmhA · Aug 19, 2019 at 04:43 PM

I had the same issue just right click on the player on the inspector and change it to prefab. That will fix the problem.

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

32 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 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

UnassignedReferenceException (Didn't find a solution) 1 Answer

[HELP C#] Making Gameobject 1 equal Prefab 2 1 Answer

How do i move a cube by one of its Vertices/Vertex 1 Answer

Why does Unity lock the transform of prefabs in my scene in playmode? 0 Answers

How to assign GameObject to a instantiated prefab via Script(C#)? 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