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 question was closed Jun 07, 2015 at 09:38 PM by Gardes for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Gardes · Apr 29, 2015 at 04:56 PM · c#arrayrandomnullreferenceexceptionbool

Accessing Instance Bool from other script

Hi, im pretty new to C# and Unity so I think my question is realy basic and simple, but i can't figure it out.

It's a litte an simple TowerDefense Game. I have some Blocks, where u are allowed to build towers. The blocks got a public bool "empty" what means you are allowed to build here and vise versa. This works perfect on all instances. As far as good.

To increase the difficult, I got now one more script. This script turns random blocks red in regular intervals. Even this works great but while they are now red, the buildplace should now set the bool from empty = false to true. I realy have no clue how to access the bool correctly.

What I tried is buildplace.empty = false; If the script happens I get this error:

NullReferenceException: Object reference not set to an instance of an object BlockThief.SeekAndDestroy () (at Assets/Scripts/BlockThief.cs:25)

This error make sense, in fact, but how can I say him he should use this instance?

Since the random block is saved in variable "block" I even tried block.empty. This wont work too. :(

Here is my Script

 using UnityEngine;
 using System.Collections;
 
 public class BlockThief : MonoBehaviour {
 
     GameObject[] blocks;
     GameObject block;
     Renderer rend;
     Buildplace buildplace;
 
     void Start()
     {
         InvokeRepeating("SeekAndDestroy",3,3);
     }
 
     void SeekAndDestroy() {
 
         blocks = GameObject.FindGameObjectsWithTag("Buildplace");
 
         if (blocks.Length != 0) {
             block = blocks [Random.Range (0, blocks.Length)];
             rend = block.GetComponent<Renderer>();
             rend.material.color = Color.red;
             block.empty = false; //Error (ye of course, but I dont know how use it correctly :( )
     }
 
 }

The other script is called Buildplace and the bool "empty" in there is public!

Hope anyone can help me :)

Cheers!

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

  • Sort: 
avatar image
1
Best Answer

Answer by DoTA_KAMIKADzE · Apr 29, 2015 at 04:57 PM

 block.GetComponent<Buildplace>().empty = false;

P.S. Or if you do something else with Buildplace script afterwards then go the same way that you've gone with Renderer.

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 Gardes · Apr 29, 2015 at 05:07 PM 0
Share

Awww, thank you so much, works great! :)

avatar image
1

Answer by brunopava · Apr 29, 2015 at 05:00 PM

 void SeekAndDestroy() 
 {
          blocks = GameObject.FindGameObjectsWithTag("Buildplace");
  
          if (blocks.Length != 0) 
          {
              block = blocks [Random.Range (0, blocks.Length)];
              rend = block.GetComponent<Renderer>();
              rend.material.color = Color.red;
              // change this:
              block.GetComponent<BuildPlace>().empty = false;
          }
 }
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 Gardes · Apr 29, 2015 at 05:07 PM 0
Share

Awww, thank you so much, works great! :)

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity event calling function gets nullreferencexception on bool 0 Answers

NullReferenceException error in an array of objects 0 Answers

Why is passing this array causing some of its values to become null? 2 Answers

One variable set TRUE when another must be false. TOGGLE 3 Answers

Randomly instantiate objects from array without choosing the same item twice. 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