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 /
  • Help Room /
avatar image
0
Question by RandomCharacters · Sep 30, 2015 at 07:10 PM · object reference

How do you set a variable to a prefab variable?

I am having a bit of an issue trying to do this using C#.

  • OK, so I have a prefab with a 'don't destroy on load'. This comes up on the main menu scene and goes though out each scene. If the player comes back to the main menu, then it checks to see if one exists so it does not recreate it. That works fine. This prefab is 'weather'.

-On the prefab I have 4 game objects that need to be set on or off. Each has its own tag 'snow-1', snow-2', rain-1' and 'rain-2'.

  • On the main menu scene I have a script that is supposed to find these prefabs by tag and turn them on or off when the player changes the setting. This gives and error.

There is some issue with how the code is set to address the instance of the object. This should be an easy fix for a skilled programmer (which I am not).

Here is the script that is attached to the weather prefab. public class WeatherDontDestroy : MonoBehaviour { ////////////////////////////////////////////////////////////////////////////////////// void Awake () { DontDestroyOnLoad(transform.gameObject); } ////////////////////////////////////////////////////////////////////////////////////// }

Here is the code on the main menu script. I just included the pertinent info.

 public class MainMenu : MonoBehaviour
 {
 
     public GameObject rain1;
     public GameObject rain2;
     public GameObject snow1;
     public GameObject snow2;
     public GameObject weatherPrefab;
 
         ////////////////////////////////////////////////
         if (!GameObject.Find("Weather"))
         {
             Instantiate(weatherPrefab, Vector3.zero, Quaternion.identity);
         }
 
         rain1 = GameObject.FindWithTag("rain1");
         rain2 = GameObject.FindWithTag("rain2");
 
         snow1 = GameObject.FindWithTag("snow1");
         snow2 = GameObject.FindWithTag("snow2");
 
         ////////////////////////////////////////////////
 
 //Set up weather with name
         switch (weatherInt)
         {
             case 1: //Clear Sky
                 rain1.gameObject.SetActive(false);
                 rain2.gameObject.SetActive(false);
 
                 snow1.gameObject.SetActive(false);
                 snow2.gameObject.SetActive(false);
 
                 text4.text = "Clear Sky";
 
                 break;



Here is the error: NullReferenceException: Object reference not set to an instance of an object MainMenu.Start () (at Assets/MiniGolf/Scripts-1/MainMenu.cs:165)

Now, it seems at the point rain1 = GameObject.FindWithTag("rain1"); it is not copying over the variable.

alt text

capture.png (9.4 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
0

Answer by RandomCharacters · Sep 30, 2015 at 08:10 PM

OK, so I found out part of the problem. The game objects in the project folder must be active at the start. They were inactive. So now they assign OK.

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

27 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

Related Questions

"Object reference not set to instance of object" but it is 1 Answer

Null Reference Exception anytime scene is reloaded? 1 Answer

How to reference the right objects when loading persisted States 0 Answers

Invisible Prefab spawning at location of Player before being instantiated 1 Answer

Hello everyone, im really new to unity and i was trying to get a sphere to jump then i got an error "An object reference is required to access non-static member". I want to learn how to fix this 0 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