• 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 therussmorris · Nov 19, 2009 at 02:52 PM · getcomponent

If/else statements inside a function...

I've inserted an if/else statement inside a function..only its not working as I want it to...as in that its not working.

It works initially, meaning that the script is calling what is in the else statement, but doesn't call up the if statement when required..

This is the part of the script Im working with...

function calc () {

 var randomize = Vector3((Random.value *1) -1, (Random.value * 2) -1, (Random.value * 1) -1);
 randomize.Normalize();
 randomize *= randomness;

 if(Input.GetKeyUp("c"))
 {
     flockCenter = Controller.GetComponent("Boid Controller").flockCenter;
     flockVelocity =  Controller.GetComponent("Boid Controller").flockVelocity;
 }

 else
 {
     flockCenter = Controller.GetComponent("Boid Controller Flock 1").flockCenter;
     flockVelocity =  Controller.GetComponent("Boid Controller Flock 1").flockVelocity;
 }

and when I press C, I get an error saying that Object reference not set to an instance of an object on this line

 flockCenter = Controller.GetComponent("Boid Controller Flock 1").flockCenter;

so it suggest that it's not looking for the new script..

Comment
Add comment · Show 1
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 Ricardo · Jan 01, 2010 at 08:02 PM 0
Share

The issue is only tangentially related to if/else statements, and it seems the problem is on your GetComponent call. Do consider changing the subject to something more relevant to the problem, so that other users can find it easily in the future.

5 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by m50 · Jan 01, 2010 at 01:43 AM

Instead of putting "c" put KeyCode.C as Bailey said.

Comment
Add comment · Show 3 · 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 Ricardo · Jan 01, 2010 at 08:00 PM 0
Share

Please add something when replying, other than "what he said".

avatar image m50 · Jan 02, 2010 at 04:55 AM 0
Share

Dear Ricardo: I was making it apparent that what Bampf had said, probably had nothing to do with the problem, but the fact that it wasn't $$anonymous$$eyCode.C. I'm sorry if this was an offence to you, but really, that was all it needed, and if the OP didn't think what Bailey said was important, then maybe he would, if other people posted the same thing. I am sorry for this, and will not let it happen again. sincerely, m50

avatar image duck ♦♦ · Mar 17, 2010 at 05:03 PM 0
Share

That's what votes are for - if you think what Bailey said was important, upvote that answer.

avatar image
0

Answer by Bailey · Dec 31, 2009 at 11:19 PM

Instead of putting "c" as your key put KeyCode.C

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 Bampf · Nov 20, 2009 at 09:33 PM

Not seeing the rest of the code makes diagnosing this a bit tricky. But I strongly suspect the problem isn't with your if-statement but with how you are handling input.

For example, let's say you call calc() in response to a key being pressed. Input.GetKeyUp will return false because the C key wasn't just released.

Doublecheck the conditions that lead to calc() being called and make sure they match the Input check inside of calc(). Better yet, change the code so it doesn't matter. One way would be for the code which is responding to the key/mouse/whatever to pass an argument to calc() that tells it what to do. For example, it could pass in the name or index of which boid controller to switch to.

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
2

Answer by TowerOfBricks · Nov 19, 2009 at 04:11 PM

If you use GetComponent(something) and you don't know if the object exists anymore it's always good to use an if after that controlling if it does exist, like this:

boidController = Controller.GetComponent("Boid Controller");
if (boidController != null) {
    flockCenter = boidController.flockCenter;
}

And just to mention that GetComponent calls is quite CPU heavy, so it's good to cache values when possible, to use less CPU, first get the Boid controller, save it to a variable, and then extract the values:

if(Input.GetKeyUp("c"))
{
    boidController = Controller.GetComponent("Boid Controller");
    if (boidController != null) {
        flockCenter = boidController.flockCenter;
        flockVelocity = boidController.flockVelocity;
    }
}
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
2

Answer by duck · Nov 19, 2009 at 03:03 PM

What's "Controller"? is that the name of a Script component that you've written yourself? Is "Boid Controller Flock 1" the name of a gameobject in your scene that has a Controller component attached?

If so, you need something along these lines:

flockObj = GameObject.Find("Boid Controller Flock 1");
flockController = (flockObj.GetComponent(Controller) as Controller);
flockCenter = flockController.flockCenter;

Although I'm getting the feeling that you may be mixing up the concept of Components and GameObjects perhaps, so if you could give a little more detail about the hierarchy and naming of your gameobjects and components, this would help a lot in solving your problem.

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 therussmorris · Nov 19, 2009 at 03:16 PM 0
Share

Controller is a gameobject that has script Boid Controller (flock 1 or just normal) attached that controls the behavior of the boids, basically what their target is.. the idea is to have multiple flocks of boids, and depending on events, the boids will move to follow a different target.

so far I've written the code that changes the controller game object (thanks for help from here)... the error comes from the script still looking for Boid Controller Flock 1, which no longer exists on the new controller, it needs to look for Boid Controller ins$$anonymous$$d... hope that makes things clearer

avatar image Ehren · Nov 19, 2009 at 04:01 PM 0
Share

Could you provide a link to your project? That would help us help you out.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

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

Can't Call function in another script 1 Answer

How am I supposed to use GetComponent? 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

C# GetComponent Issue 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