need to steadily expand primitive

Here is my script to make the sphere expand when “p” is pressed. I’m new to Unity and don’t know where I went wrong.

#pragma strict

var speed : float = 10f;
var SphereSize : float = 10;
var SphereExpansion : float = 1.0;


function Start () {

	var sphere : GameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);
	
		sphere.transform.position = Vector3(0,0,0);
		
		sphere.transform.localScale = Vector3(10,10,10);
	
}

function Update () {

		if(Input.GetKeyDown("p")){
			
			var sphere = GameObject.Find("sphere");
			
				SphereSize += SphereExpansion * speed * Time.deltaTime;
			
				sphere.transform.localScale = Vector3.one * SphereSize;	
					
						
				
		}
		
}

I am not getting an error, but the sphere won’t budge.

At a glance, you’re using GetKeyDown, which only returns true on the first frame the key was pressed.

For what it’s worth, letting us know what you’ve already tried differently is a good idea. It shows us that you’re not just asking us to do the problem solving for you. :wink:

First off, you are asking unity to find sphere on each frame while p is pressed. Not good, you see, you should only ask once. Do so at start or some other key moment.

What exactly are your results?

Thanks, I thought that GetKeyDown had a different function than what it turns out to be. And next question I’ll be sure to let you guys know I’ve spent 4 hours trying to do this :slight_smile:

^ A sphere that doesn’t move

What exactly do you mean when you say I can put it at start? I would think that it would only work for one frame, and after that, it wouldn’t update.

It doesn’t matter whether it moves. It’s not finding it in physical space.

In any case, while renman3000 is right that it’s good practice to cache things that’s not the problem here.

You don’t need to “find” it at all. Just keep a reference to the sphere when you create it in the Start function.

But… that still isn’t your problem. So ignore that distraction for now and try the thing I said that’s actually related to the thing that’s not working. :wink:

var sphere = gameObject.GetComponent(Transform);
		
		if(Input.GetKeyUp("p")){
			
				SphereSize += SphereExpansion * speed * Time.deltaTime;
			
				sphere.transform.localScale = Vector3(SphereSize, SphereSize, SphereSize);

Still no luck. I tried using GameObject.GetComponent(Transform), and I still think this may be my solution. Again, no error, but no luck.

This makes more sense to me:

if(Input.GetKeyUp("p")){
		

			var sphere = GameObject.Find("sphere");
			
			var transform = gameObject.GetComponent(Transform);
			
				sphere.transform.position = Vector3(0,0,0);
			
				SphereSize += SphereExpansion * speed * Time.deltaTime;
			
				sphere.transform.localScale = Vector3(SphereSize, SphereSize, SphereSize);	
					
						
				
		}

Still nothing, so I’m guessing this is not what you meant when you said “find in physical space”?

No, what I meant by “it’s not finding it in physical space” is that the engine doesn’t care whether something is moved or not because it doesn’t look for things based on where they are positioned in the scene. It looks for them by traversing all of the data that represents the scene until it finds a match. That’s important to know, but…

… it has nothing to do with your problem here. Put it back how it originally was until you’ve got the first problem solved. This thing about finding it more efficiently is a distraction.

You’re checking input incorrectly. GetKeyUp only returns true on the frame that the key goes up. You should check out more of the docs. There are other Input methods that might be useful, and the docs tell you what each one does. Here’s a link.

Alright, I think I understand now. So just to be clear, I don’t need the transform variable I just made, and I had everything ,but the input, correct?

Sorry, my bad, I thought his code triggered over and over per frame, and not a one shot deal. Still, asking to find an object each time you press down, is probably not the most effecient way. Cache the item, or store the item, means you get the item once, generally speaking. Doing this saves overhead on the engine.

var sphere = GameObject.Find("sphere");

…will not find your instantiated sphere GameObject. The default sphere primitive that you instantiate will be named “Sphere”, not “sphere”.

Input.GetKeyUp() will only work when the P key is released. I’m not sure if that’s your intended behaviour? If you want it to expand while the P key is held, use Input.GetKey().

Well, he wants it to continue like that. However, that wasn’t the problem he was asking about, and just served to confuse things and distract from the actual problem.

It’s a classic case of premature optimisation. Something doesn’t work, but instead of fixing it you’re concentrating on making something else faster.

Make it fast after you’ve made it work. Then, if and only if the speed is important, is when you go and optimise it.

Further up I have

var sphere : GameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere); /

#pragma strict

 

var speed : float = 10f;

var SphereSize : float = 10;

var SphereExpansion : float = 1.0;

 

 

function Start () {

 

    var sphere : GameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);

    

        sphere.transform.position = Vector3(0,0,0);

        

        sphere.transform.localScale = Vector3(10,10,10);

    

}

 

function Update () {

 

        if(Input.GetKeyDown("p")){

            

            var sphere = GameObject.Find("sphere");

            

                SphereSize += SphereExpansion * speed * Time.deltaTime;

            

                sphere.transform.localScale = Vector3.one * SphereSize; 

                    

                        

                

        }

        

}

I thought this would change the tag to sphere?

I think maybe you’re confusing GameObject.name with variable names.

I could do this:

 var joe: GameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);

joe.name = "Jimmy";

GameObject.Find(“joe”) is going to get me nothing.
GameObject.Find(“Jimmy”) will get me something.

The stuff they said about efficiency, would be skipping the Find call and just using the variable directly, but you’d need to define the variable outside the scope of the function. Right now you’re defining it inside the function so it’s not accessible from other functions. Stuff like SphereSize and speed you’re defining outside the function. Then you just assign it a value in the Start function.

Haha, that’s pretty important actually. Ignoring how fast it works, making sure that you’re finding the thing you think you’re finding is important.

Personally I’d put a Debug.Log(sphere.name) just before I tried to modify its properties so I could see if it was being found. (I’d expect a null reference if it was finding nothing, but it could be finding the wrong thing.)

I FINALLY JUST GOT THIS TO WORK! I did it a little different from the advice I was offered, but now I understand exactly what the problem was. Thanks to you all!