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 immafirinmahlazor · Mar 17, 2017 at 03:32 PM · scripting problemcomponents

how to get a component from another script and affect the changes on it as well

hello, im trying to make two separate scripts for my platformer project in school; they are for the spawning of enemies and the other is for the counter to how many enemies they could respawn before the boss arrives. here are my scripts

enemyrespawncontroller:

 public Transform OnePrefab;
 public Transform TwoPrefab;
 public Transform ThreePrefab;
 public Transform FourPrefab;
 public int x;
 public float upperlimit = 4.0f;
 public float lowerlimit = -1.0f;
 public float everyseconds = 2.0f;
 public float respawnlimit = 10.0f;
 public float delay = 0;
 public enemycountercontroller enemycoun = null;

 void Start () {
     enemycoun = GetComponent<enemycountercontroller> ();
 }

 void FixedUpdate () {
     if (Time.time % everyseconds == 0) {
         Invoke ("GenerateEnemy", delay);
         enemycoun.z = enemycoun.z - 1;
     }
 }
 void GenerateEnemy(){
     x = Random.Range (0, 4);
     if (x == 0) {
         Instantiate (OnePrefab, new Vector2 ( respawnlimit, Random.Range (lowerlimit, upperlimit)), OnePrefab.rotation);
     } else if (x == 1) {
         Instantiate (TwoPrefab, new Vector2 ( respawnlimit, Random.Range (lowerlimit, upperlimit)), TwoPrefab.rotation);
     } else if (x == 2) {
         Instantiate (ThreePrefab, new Vector2 ( respawnlimit, Random.Range (lowerlimit, upperlimit)), ThreePrefab.rotation);
     } else if (x == 3) {
         Instantiate (FourPrefab, new Vector2 ( respawnlimit, Random.Range (lowerlimit, upperlimit)), FourPrefab.rotation);
     } 
 }

}

(Note that i have a clone script called groundenemyrespawncontroller, which functions the same as enemyrespawncontroller)

and here is the enemycountercontroller:

 public int z = 100;

 void Start () {
     
 }
 

 void Update () {
     if (z <= 0) {
         
     }
 }

}

-assume if z reaches 0, then boss appears

i tried those, but the counter didnt move down. and there is an error every time it spawns:

NullReferenceException: Object reference not set to an instance of an object groundenemyrespawncontroller.FixedUpdate () (at Assets/scripts/groundenemyrespawncontroller.cs:24)

how do i fix this problem please answer asap thanks

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 Commoble · Mar 17, 2017 at 03:36 PM 0
Share

Can we see your groundenemyrespawncontroller script?

avatar image immafirinmahlazor · Mar 17, 2017 at 03:40 PM 0
Share

Groundenemyrespawncontroller is much the same as enemyrespawncontroller

avatar image Commoble immafirinmahlazor · Mar 17, 2017 at 03:45 PM 0
Share

Your error is in groundenemyrespawncontroller. If you don't show us the script your error is in. we can't help you. You should also post your code starting from the very top of the script so we can see which line the error is in.

avatar image immafirinmahlazor Commoble · Mar 17, 2017 at 03:54 PM 0
Share

What i mean is groundenemyrespawncontroller is exactly a clone of the enemyrespawncontroller. It just so happens the error from groundenemy went first. But they are both exactly the same.

Also when you mean "post your code starting from the very top of the script", did you mean including the libraries used?

Show more comments
avatar image Pengocat · Mar 17, 2017 at 04:13 PM 0
Share

The problem you have is probably how the GameObject and components are put together. Is all the scripts on the same GameObject? GetComponent<> only looks at the GameObject this script is on.

0 Replies

· Add your reply
  • Sort: 

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

If several BoxCollider attach to a gameObject, how to differ them? 0 Answers

Trigger enabling component works in one statement but not another? 1 Answer

No overload for method. 1 Answer

Why won't script work on 3d text? (menu script) 1 Answer

Scrpits component disappears when I enter playmode 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