Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ibrahimmunaser · Dec 26, 2015 at 04:57 AM · c#scripting problemerrorrandomrandom.range

My Script Wont work?

Hi I have a script attached to the main camera, and in this script I want to choose a number between 0 to 5 . And depending on what number I get, I want a script to run. Hers my script that is attached to the main camera. I keep getting this error

NullReferenceException: Object reference not set to an instance of an object RandomFunction.Start () (at Assets/Resources/Scripts/RandomFunction.cs:22)

 using UnityEngine;
 using System.Collections;

 public class RandomFunction : MonoBehaviour {

     int n;



     void Awake()
     {
         GetComponent<RandomFunction> ().enabled = true;
     }

     void Start () 
     {
         n=Random.Range(0,5);


         if(n==0)
         {
             GetComponent<BlueGoUp> ().enabled = true;
         }
         else if(n==1)
         {
             GetComponent<RedGoUp> ().enabled = true;
         }
         else if(n==2)
         {
             GetComponent<GreenGoUp> ().enabled = true;
         }
         else if(n==3)
         {
             GetComponent<OrangeGoUp> ().enabled = true;
         }
         else if(n==4)
         {
             GetComponent<YellowGoUp> ().enabled = true;
         }
         else if(n==5)
          {
             GetComponent<PurpleGoUp> ().enabled = true;
         }
     }
 
 }


please help.. I dont know why its doing this. The script im telling it to run, wont run.

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
2

Answer by allenallenallen · Dec 26, 2015 at 05:01 AM

First of all, this line:

 GetComponent<RandomFunction> ().enabled = true;

You can't enable the script itself. If this script is disabled from the very beginning, it won't enable itself via code because it's already disabled.

The other problems are your GetComponents for other scripts. Be sure to assign them correctly.

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 Kimi073 · Dec 26, 2015 at 05:54 AM

You haven't defined and assigned the [...]GoUp yet.

You should define it by saying:

 public GameObject  GreenGoUp;
 public GameObject RedGoUp;
 //Others just add public GameObject before it

Then in the inspector which holds this script, assign the game object which you consider it to be GreenGoUp or RedGoUp or whatever else by dragging that game object in the hierarchy to the desire empty slot

Secondly if you want to create random number you, in this case, you should take it as

 Random.Range(0,6);

in order to generate the number 5 or else it only generate random number up to 4.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity can't use the script. 0 Answers

Hey guys, im starting do make a RPG game and in the Level system script(I called it "PlayerStatsController") i have an error: Parser Error : Unexpected symbol `public'. My script above: 1 Answer

UnityIAP? Assembly [...] will not be loaded due to errors: 0 Answers

i auto generated a input manager c# script like Brackey's video and i am now getting compiler errors even though i did the exact same thing as him. Very confused. 2 Answers

Non-repeating random number generator crashing unity 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