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 Party Boy · Mar 25, 2014 at 07:00 PM · c#inspectorlight

Variable has not been assigned error

Hey. I'm trying to shoot a light from a magic wand, and I keep getting this error when I run the game. The light still comes out but the error appears in the console. I created a light prefab and I dragged that onto my script "Light_Spell". Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class Light_Spell : MonoBehaviour {
 
     private Light wandLight;
     private bool triggerIsPressed;
 
     private float life = 1.0f;
     private float speed = 15.0f;
     private float timeToDie = 1.0f;
 
     [SerializeField]
     public Light lightProjectile;
 
     // Find the light object and disable it
     void Start () {
         wandLight = GameObject.Find ("WandLightSpell").light;
         wandLight.enabled = false;
         triggerIsPressed = false;
     }
     
     // Handles button presses etc
     void Update () {
 
         if (SixenseInput.Controllers [0].Trigger != 0) {
             triggerIsPressed = true;
             wandLight.enabled = true;
             wandLight.intensity = (float)(SixenseInput.Controllers [0].Trigger) * 3;
             wandLight.range = (float)(SixenseInput.Controllers[0].Trigger) * 5;
         }  
         else {
             triggerIsPressed = false;
             wandLight.enabled = false;
             //wandLight.intensity = 0;
             //wandLight.range = 0;
         }
 
         if (SixenseInput.Controllers [0].GetButtonDown (SixenseButtons.BUMPER) && triggerIsPressed == false) {
             var instantiateProjectile = Instantiate(lightProjectile, transform.position, transform.rotation) as Light;
 
             //instantiateProjectile.transform.TransformDirection(new Vector3(5,5,5));
         }
     }

Anybody see what I'm doing wrong here? Also the light is shooting upwards, how can I have it shoot wherever the wand is pointed at? Thanks!

Comment
Add comment · Show 5
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 Owen-Reynolds · Mar 26, 2014 at 12:50 AM 0
Share

It should also say which variable, and give a line number?

avatar image Party Boy · Mar 26, 2014 at 01:02 AM 0
Share

@OwenReynolds It is around line 40 where the instantiateProjectile business is happening. It says Light projectile

avatar image Party Boy · Mar 26, 2014 at 01:03 AM 0
Share

@OwenReynolds specific error is:

NullReferenceException: Object reference not set to an instance of an object Light_Spell.Update () (at Assets/Scripts/Spells/Light_Spell.cs:37)

avatar image Simon-Larsen · Mar 26, 2014 at 01:51 AM 0
Share

This may not make any difference whatsoever, but I'm just wondering. Why are you instantiating a Light component and not a GameObject with a Light component attached?

avatar image Party Boy · Mar 26, 2014 at 02:07 AM 0
Share

@SimonLarsen I'm new to all this so I'm not sure why I would do it. What makes it better to do it that way? Do you mean a prefab? I made a light prefab and I have tried instantiate it and I think it fixed the error, however the light just floats above me and disappears not sure how to fix it

2 Replies

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

Answer by Owen-Reynolds · Mar 26, 2014 at 02:49 PM

I think it's just a matter of trying something a little too complicated, before you have more practice with Unity, and especially with coding. Maybe try something simpler first. Either another part of your project, or a simple version of this.

The line number you've got, 37, is a }. That probably means the code above is no longer the code you're running. After a bit, you'll get used to double-clicking on the errors, and what they mean.

There are a lot of examples how to use Instantiate. This one is a little long, and somewhat strange (the Start code looks for a pre-existing light, but the button code creates a new light?)

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 Party Boy · Mar 26, 2014 at 05:50 PM 0
Share

@OwenReynolds I understand what you mean. I'm still learning. Yeah I noticed that bit of odd code after you said it and removed it. However I don't know how to fix the instantiaten problem

avatar image Owen-Reynolds · Mar 26, 2014 at 11:18 PM 0
Share

Start with a working Instantiate, from the Unity docs. Then, when you change one thing and it stops working, you'll know it was that one thing.

Or turn what you have into a 6-line program. When you press the button, an (already turned on, etc...) light pops up. Once that works, add back in ai$$anonymous$$g, timeToDie... and test as you go.

avatar image
0

Answer by Pablo_G · Mar 27, 2014 at 07:09 AM

After every "private" or "public" type in var ex. private var Light wandLight; public var Light lightProjectile;

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 Owen-Reynolds · Mar 27, 2014 at 01:24 PM 0
Share

That's for Unityscript. C# allows the word var, but if you already have the type, the var is just extra letters.

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

23 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

c# enum wont show in inspector 3 Answers

Input variable from Inspector? 1 Answer

Draw more EditorGUILayout components? 1 Answer

Flashlight turning off, but not back on again? 4 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